Search for the maximum sum of a subarray with a length constraint
Rust
Hard
vectors
Task «Search for the maximum sum of a subarray with a length constraint»
Given an integer vector and a number k. Find the maximum sum of any subarray of length at most k. If there are multiple such subarrays, output the sum of the first one in order. Constraints: vector length from 1 to 10^5, k from 1 to the length of the vector, numbers from -10^9 to 10^9. The solution must be efficient (O(n)).
Input Format
First line: number n (vector length) and k (maximum subarray length). Second line: n integers separated by spaces.
Output Format
One integer — the maximum sum of a subarray of length at most k.
Examples
Example 1
INPUT
6 4
1 -2 3 4 -5 6
OUTPUT
8
Example 2
INPUT
delivered
[{"id":10,"status":"processing","items":[]}]
OUTPUT
[{"id":10,"status":"processing","items":[]}]
main.rs