#P2889. [USACO07NOV] Milking Time S

[USACO07NOV] Milking Time S

Description

Bessie can produce milk in the next NN hours. For convenience, we number these NN hours 1N1 \dots N.

Within these NN hours, FJ has MM intervals during which he can milk Bessie. The ii-th interval runs from StartiStart_i to EndiEnd_i and yields EffiEff_i gallons of milk.

After each milking, Bessie must rest for RR hours before FJ can start the next milking.

Now, FJ needs you to compute the maximum total milk Bessie can produce within these NN hours.

Input Format

The first line contains three integers, representing NN, MM, and RR.

Lines 2M+12 \dots M+1: the (i+1)(i+1)-th line contains three integers StartiStart_i, EndiEnd_i, and EffiEff_i, describing one milking interval.

Output Format

Output a single integer on one line: the answer.

12 4 2
1 2 8
10 12 19
3 6 24
7 10 31
43

Hint

Constraints

For all testdata, it is guaranteed that 1N1061 \le N \le 10^6, 1M1031 \le M \le 10^3, 1Starti<EndiN1 \le Start_i < End_i \le N, and 1Effi1061 \le Eff_i \le 10^6.

Translated by ChatGPT 5