Run, Run
Time limit2sMemory limit128 MB
Find the maximum distance covered in N minutes of running and forced resting under a fatigue cap M, using dynamic programming over run-rest blocks.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Prefix sum, Simulation
- Solved
- No attempts yet
Problem
The students have already completed two group running sessions on the previous two days. After that hard training, they can now control their pace so that a workout lasts exactly N minutes.
During each of the next N minutes, the students choose either to run or to rest. Their fatigue starts at 0. Running for one minute increases fatigue by 1, and resting for one minute decreases fatigue by 1. If fatigue is already 0, resting keeps it at 0. Fatigue may never exceed M, so the students cannot run when their fatigue is M.
If they run during the i-th minute, they cover D_i distance. This means the one-minute interval numbered i, not that they have run for i minutes total. Once they begin resting while their fatigue is positive, they must keep resting until their fatigue becomes 0.
When all N minutes are over, their fatigue must also be exactly 0. Find the maximum total distance they can cover.
Input
The first line contains the workout time N (1 ≤ N ≤ 10000) and the maximum fatigue M (1 ≤ M ≤ 500). Each of the next N lines contains D_i (0 ≤ D_i ≤ 10000), the distance covered if the students run during the i-th minute, in order from 1 to N.
Output
Print the maximum distance that can be covered while satisfying all conditions.
Hint
One optimal plan is to run 5 distance in the first minute, rest during the second minute to return fatigue to 0, run 4 more distance in the third minute, and then rest during the fourth and fifth minutes. The total distance is 9.