Problem statement
Given an array and a distance k, choose a subsequence whose chosen indices are at most k apart, and whose values add up as high as possible.
A subsequence may start anywhere, so a negative earlier sum is simply not extended. The best sum ending at each index is one value, and it needs the best of the previous k of them.
Examples
Input[10, 2, -10, 5, 20], k = 2
Output37
The best run is 10, 2, 5, 20, whose indices are never more than two apart.
Input[7], k = 1
Output7
One value is a subsequence on its own, so the answer is that value.
The walkthrough is part of Premium
The statement and the examples above stay free. The full how-to-solve walkthrough, the cost notes in plain words and the interactive step-by-step visualizer unlock with a Premium plan.
See pricing