This page is still under construction.

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

Yonsei Water Park

Interview

Time limit1sMemory limit128 MB

Summary
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 NN stepping stones with an integer KiK_i 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 NN stones are numbered 11 through NN in order. To jump from stone UU to stone VV, the difference between UU and VV must be at most a fixed value DD.
  • 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 NN and the value DD described above. (2≤N≤1052 \le N \le 10^5, 1≤D≤N−11 \le D \le N-1)

Then NN integers follow, in order from stone 11 to stone NN. The number written on stone ii is KiK_i. (−109≤Ki≤109-10^9 \le K_i \le 10^9)

Output

Print the largest score that is possible.

Examples2

  1. Example 1

    Input
    10 2
    2 7 -5 -4 10 -5 -5 -5 30 -10
    
    Expected output
    40
    
  2. Example 2

    Input
    3 2
    -4 -2 -7
    
    Expected output
    -2