#P1353. [USACO08JAN] Running S

    ID: 350 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>动态规划,dp2008USACO福建省历届夏令营

[USACO08JAN] Running S

Description

The cows plan to develop their athleticism through exercise. As one of them, Bessie chooses to do a morning run for nn minutes every day. At the beginning of each minute, Bessie chooses whether the next minute will be used for running or resting.

Bessie’s stamina limits how far she can run. Specifically, if Bessie chooses to run during minute ii, she can run did_i meters in that minute, and her fatigue increases by 11. However, at any time her fatigue must not exceed mm.

If Bessie chooses to rest, her fatigue decreases by 11 per minute, but she must continue resting until her fatigue returns to 00. If she rests when her fatigue is 00, the fatigue level will not change further. At the start of the run, Bessie’s fatigue is 00.

Also, at the end of the nn minutes of exercise, Bessie’s fatigue must be 00 as well, or she will not have enough energy to handle the rest of the day.

Please compute the maximum total distance Bessie can run.

Input Format

The first line contains two positive integers n,mn, m.
The next nn lines each contain one positive integer did_i.

Output Format

Output a single integer, the maximum distance Bessie can run while satisfying all constraints.

5 2
5
3
4
2
10

9

Hint

Constraints
For 100%100\% of the testdata, 1n1041 \le n \le 10^4, 1di10001 \le d_i \le 1000, 1m5001 \le m \le 500.

Sample explanation:

Bessie runs in minute 11 (running 55 meters), rests in minute 22, runs in minute 33 (running 44 meters), and rests for the remaining time.
Because her fatigue must be 00 at the end of the morning run, she cannot choose to run in minute 55.
The total distance run is 99.

Translated by ChatGPT 5