#169Hard
Maximum subarray sum with length limits
Time: 1 s · Memory: 256 MB
- Prefix sums
Statement
You are given an array of integers. Find the maximum sum of a subarray whose length is between and .
Input format
The first line contains , , (). The second line contains integers ().
Output format
A single number — the maximum sum.
Examples
Input
8 1 2 -1 3 -2 5 3 -5 2 2
Output
8
Note
The sum of is , and the length condition is . So for every you need the minimum prefix in the window — a sliding window minimum. Keep it in a monotonic deque with amortised per step (or a multiset with ).
Scanning the window for every is — too slow when is large. In the example .
Source: CSES, problem 1644.
