863. Hopping Forward With a Limited Stride
A row of n squares is numbered 0 to n-1; square i is worth a[i], which may be negative.
You start on square 0 and must finish on square n-1. From square i you may hop to any square
j with i < j <= i + k. Your score is the total of every square you LAND ON, including the
first and the last.
Print the largest score.
n = 6, k = 2, values 1 -1 -2 4 -7 3 -> 7
n = 6, k = 2, values 10 -5 -2 4 0 3 -> 15
n = 5, k = 1, values 1 2 3 4 5 -> 15
The first row is 1 -> -1 -> 4 -> 3, giving 1 - 1 + 4 + 3 = 7. With k = 1 there is no choice
at all and the answer is the total of everything.
## The dynamic program, and why it is too slow
dp[0] = a[0]
dp[i] = a[i] + max( dp[i-k] ... dp[i-1] )
which is correct and costs O(nk). With n = 200000 and k = 200000 that is 4 * 10^10. The
recurrence is fine; the repeated maximum over a sliding range is the problem.
## The deque, and why it decreases
Keep a DOUBLE-ENDED queue of indices whose dp values decrease from front to back. Two rules:
before reading: drop from the FRONT any index older than i - k
after computing: drop from the BACK every index whose dp is <= dp[i], then push i at the back
The front then always holds the index with the largest dp inside the window, so dp[i] is read
in constant time.
Popping from the back is the familiar domination argument in its cleanest form. If j < i and
dp[j] <= dp[i], then j is worse than i on value and leaves the window earlier than i, so
there is no future query for which j is the answer and i is not. It is discarded permanently.
Popping from the front is the only thing a deque adds over a stack: entries leave not because something better arrived but because they are too old. That is the whole reason this family needs two ends.
## Why a heap is the wrong tool here
A priority queue also answers "largest in the window", by storing pairs and discarding stale tops
lazily. It works, and it costs O(n log n) with a large constant. The deque is O(n) with a tiny
one, because the window is a contiguous range and the ordering of removals is known in advance.
## What goes wrong
The front must be expired BEFORE reading it, not after pushing. The back pop should use <= so
that equal values do not accumulate; with < the deque grows but the answer stays correct, which
makes it an efficiency bug rather than a correctness one.
Values are negative, so no accumulator may start at 0, and dp must be seeded with dp[0] = a[0]
rather than with 0. The row of all -10000 is the test that catches it.
## Related problems
Problem 194 uses the same deque to report the minimum of every window of a FIXED size over the
input array. Here the window slides over the dp array, which is being built as the deque is
used, and that self-reference is what makes it a harder use of the same tool.
## Cost
Time O(n): each index enters and leaves the deque once. Space O(n).
Constraints - `1 ≤ k ≤ n ≤ 200000` - `-10^4 ≤ a[i] ≤ 10^4` - the answer can be negative
Input
The first line contains two integers n and k.
The second line contains n integers a[0] ... a[n-1].
Output
Print one integer alone on a line: the largest total of the squares landed on, from square 0 to square n-1 inclusive. Print no other words.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.