Forests of Fangorn

Time limit: 2.0 sec
Memory limit: 256 MB

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 $1$ to $N$. Each stone $i$ holds a magic value $A_i$. The wanderer starts on the first stone (stone $1$) and must reach the last stone (stone $N$). However, the magic within each stone is delicate; the wanderer can only jump from stone $i$ to stone $j$ if the condition $$i < 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 $N$. Your task is to compute the maximum magic points that can be accumulated on a valid journey from stone $1$ to stone $N$. ## Input Format - The first line contains two integers, $N$ and $K$, where - $N$ is the number of mystic stones, and - $K$ is the maximum allowed jump length. - The second line contains $N$ space-separated integers, $A_1, A_2, \dots, A_N$, where $A_i$ is the magic value of stone $i$. ## 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: