Problem 5

Forests of Fangorn

Data Structures, Math, Dynamic Programming

Problem Statement

In the ancient forests of Fangorn, legends speak of a series of mystic stones imbued with magical energy. A brave wanderer has set out on a journey to collect these energies to unlock a long-forgotten power. The stones are arranged in a straight line and numbered from 11 to NN. Each stone ii holds a magic value AiA_i.

The wanderer starts on the first stone (stone 11) and must reach the last stone (stone NN). However, the magic within each stone is delicate; the wanderer can only jump from stone ii to stone jj if the condition
i<ji+Ki < j \leq i + K
is met. On landing at a stone, the wanderer collects its magic value. His task is to maximize the total magic points he can gather by the time he reaches stone NN.

Your task is to compute the maximum magic points that can be accumulated on a valid journey from stone 11 to stone NN.

Input Format

  • The first line contains two integers, N5000N \leq 5000 and K5000K \leq 5000, where
    • NN is the number of mystic stones, and
    • KK is the maximum allowed jump length.
  • The second line contains NN space-separated integers, 1000A1,A2,,AN1000-1000 \leq A_1, A_2, \dots, A_N \leq 1000, where AiA_i is the magic value of stone ii.

Output Format

Print a single integer denoting the maximum total magic points the wanderer can collect while reaching the last stone.

Examples

Input

5 2
1 2 3 4 5

Output

15

Input

3 1
10 -5 20

Output

25

Code Editor

Language
Theme