Yonsei Water Park
InterviewTime limit1sMemory limit128 MB
Given N stones in a line with values K_i, pick a starting stone and a sequence of distinct stones where each jump moves at most D positions, maximizing the sum of visited values.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Segment tree, Array
- Solved
- No attempts yet
Problem

(Yonsei University library, July 2016)
Every summer a surprise water park opens at Yonsei University. Nobody knows where it will appear, and the only known pattern is that it usually shows up near the library or the west gate.
The university decided that stopping the opening is too hard, so it laid out stepping stones with an integer written on each one, to let students enjoy the water park more. Junho, walking home with his friends after class, came up with a game several people can play on those stones.
- Each player picks any one stone as a starting point.
- Starting there, the player keeps jumping and steps on as many stones as they like, then leaves whenever they want. Leaving right at the starting point is allowed.
- The player whose stepped stones, the starting point included, have the largest sum of written integers wins.
After watching a friend jump in place to reach a billion points, Junho added more rules.
- The stones are numbered through in order. To jump from stone to stone , the difference between and must be at most a fixed value .
- No stone may be stepped on more than once.
The game now runs under the new rules. What is the largest score Junho can get?
Input
The first line contains the number of stepping stones and the value described above. (, )
Then integers follow, in order from stone to stone . The number written on stone is . ()
Output
Print the largest score that is possible.