Problem 902
Jump Game VI
Medium · 20 pts · Array, Dynamic Programming, Monotonic Queue
Problem
You start at index 0 of an array and want to reach the last index. From index i you can jump to any index from i + 1 to i + K. Your score is the sum of the values at every index you visit, including the first and the last. Maximise the score. Let best[i] be the best score ending at i; it is NUMS[i] plus the maximum of best over the previous K indices, which a monotonic deque provides in O(1) amortised.
Input
The first line contains N and K. The second line contains the N values.
Output
Print the maximum score.
Constraints
1 <= N, K <= 100000; -10^4 <= NUMS[i] <= 10^4
Example input
6 2 1 -1 -2 4 -7 3
Example output
7
Explanation
For 1 -1 -2 4 -7 3 with K = 2, visit indices 0, 1, 3 and 5 for a score of 1 - 1 + 4 + 3 = 7, the best possible.