A ledger records n integer daily balance CHANGES (each can be positive, negative, or zero). For a fixed window length k, consider every contiguous window of k consecutive days. In each window, find the FIRST (leftmost) daily change that is STRICTLY NEGATIVE within that window; call this the window's "first deficit". If a window has no negative entries at all, its first deficit counts as 0.
Print the SUM, over all windows, of their first deficits.
Line 1: two integers n and k.
Line 2: n space-separated integers, the daily changes.
A single integer: the sum described above.
Example 1
Input
5 3 5 -2 3 -4 1
Expected
-8
Explanation
Windows [5,-2,3], [-2,3,-4] and [3,-4,1] have first deficits -2, -2 and -4 respectively, summing to -8.
Example 2
Input
4 2 1 2 3 4
Expected
0
Explanation
There are no negative changes at all, so every window contributes 0 and the total is 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →