#P8387. [COI 2021] Autobahn

[COI 2021] Autobahn

Problem Description

Translated from COI 2021 T1 “Autobahn

There are NN people racing on a track. Person ii races from the beginning of hour lil_i to the end of hour rir_i.

Since the track wants to make money, it charges one unit of money for each hour of racing. However, these NN people have only paid for the first tit_i hours.

The administrators are very kind: they will only come to collect additional money when there are at least KK people racing on the track.

The track runs a promotion: it must choose a time interval of length XX hours. During this interval, if someone would need to pay extra money, then they do not need to pay the unpaid money corresponding to this interval.

The track wants this promotion to maximize the total amount of money that these NN people do not need to pay extra. Find this maximum amount.

Input Format

The first line contains three integers NN, KK, and XX.

The next NN lines each contain three integers lil_i, tit_i, and rir_i.

Output Format

Output a single integer, the answer.

5 3 4
2 1 4
3 3 7
3 3 8
1 5 7
5 3 8

7
3 2 22
7 16 33
69 14 88
8 10 97
27

Hint

[Sample Explanation]

Sample #1 explanation:

The chosen interval is [4,7][4,7]. In it, the first person does not need to pay the fee for hour 44, and the second, third, and fourth people do not need to pay the fees for hours 66 and 77.

Sample #2 explanation:

The chosen interval is [12,33][12,33].

[Constraints]

For 100%100\% of the testdata, 1KN1051\le K\le N\le 10^5, 1X,li,ti,ri1091\le X,l_i,t_i,r_i\le 10^9, and liril_i\le r_i.

Subtask Constraints Score
11 N,K,X,li,ti,ri100N,K,X,l_i,t_i,r_i\le 100 2020
22 N,K,X,li,ti,ri103N,K,X,l_i,t_i,r_i\le 10^3 3030
33 No additional constraints 5050

Translated by ChatGPT 5