Forests of Fangorn
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 to . Each stone holds a magic value .
The wanderer starts on the first stone (stone ) and must reach the last stone (stone ). However, the magic within each stone is delicate; the wanderer can only jump from stone to stone if the condition
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 .
Your task is to compute the maximum magic points that can be accumulated on a valid journey from stone to stone .
Input Format
- The first line contains two integers, and , where
- is the number of mystic stones, and
- is the maximum allowed jump length.
- The second line contains space-separated integers, , where is the magic value of stone .
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
Submit a solution. Authentication is required to create submissions.