This page is still under construction.

Parts of this page are still being built. What you see may change.

Milking Time

Interview

Time limit1sMemory limit128 MB

Summary
Choose non-overlapping milking intervals, each separated by at least R rest hours, to maximize the total milk produced over N hours.
Level

Medium6 of 10

Topics
Dynamic programming, Binary search, Sorting, Intervals
Solved
No attempts yet

Problem

Bessie is a very hard-working cow, and she wants to maximize how much milk she produces. She plans her next NN (1≤N≤1061 \le N \le 10^6) hours, labeled 0…N−10 \dots N-1, so that she produces as much milk as possible during them.

Farmer John has a list of MM (1≤M≤10001 \le M \le 1000) possibly overlapping intervals during which he is available for milking. Interval ii has a starting hour sis_i (0≤si<N0 \le s_i < N), an ending hour eie_i (si<ei≤Ns_i < e_i \le N), and an efficiency wiw_i (1≤wi≤1061 \le w_i \le 10^6), which is the number of gallons of milk Bessie produces during that interval. Milking starts at the beginning of the starting hour and stops at the beginning of the ending hour. If Bessie is milked during an interval, she must be milked through the entire interval.

After being milked during any interval, Bessie must rest RR (1≤R≤N1 \le R \le N) hours before she can start milking again. That is, if she is milked during an interval that ends at hour ee, the next interval she is milked in must start at hour e+Re + R or later. Given Farmer John's list of intervals, determine the maximum amount of milk Bessie can produce during the NN hours.

Input

  • Line 1: three space-separated integers NN, MM, and RR.
  • Lines 2 to M+1M+1: line i+1i+1 describes the ii-th milking interval with three space-separated integers sis_i, eie_i, and wiw_i.

Output

  • Line 1: the maximum number of gallons of milk Bessie can produce during the NN hours.

Examples2

  1. Example 1

    Input
    12 4 2
    1 2 8
    10 12 19
    3 6 24
    7 10 31
    
    Expected output
    43
    
  2. Example 2

    Input
    20 2 2
    0 5 10
    7 12 20
    
    Expected output
    30