#C5902. Maximum Subarray Sum with Minimum Length
Maximum Subarray Sum with Minimum Length
Maximum Subarray Sum with Minimum Length
Given an array of integers \(arr\) and an integer \(k\), your task is to find the maximum sum of any contiguous subarray that has a length of at least \(k\). In other words, among all subarrays with length \(\ge k\), choose the one with the largest sum and return that sum.
Constraints:
- \(1 \leq k \leq n\), where \(n\) is the number of elements in the array.
- Elements in \(arr\) can be positive, negative, or zero.
Note: When all elements are negative, the result will be the best (least negative) sum among all subarrays of length at least \(k\). You may use sliding window and dynamic programming techniques to achieve an efficient solution. The sum of a subarray \(arr[i \dots j]\) is given by \(\sum_{p=i}^{j} arr[p]\).
inputFormat
The first line contains two integers \(n\) and \(k\) separated by a space, where \(n\) is the number of elements in the array, and \(k\) is the minimum required subarray length.
The second line contains \(n\) integers separated by spaces representing the elements of the array \(arr\).
outputFormat
Output a single integer which is the maximum sum of any contiguous subarray with length at least \(k\).
## sample6 2
1 -2 3 4 -5 6
8