#K47917. Maximum Contiguous Subarray Sum with Limited Length
Maximum Contiguous Subarray Sum with Limited Length
Maximum Contiguous Subarray Sum with Limited Length
You are given an array of n integers and an integer l representing the maximum allowable length of a contiguous subarray. Your task is to compute the maximum sum of any contiguous subarray whose length is at most l.
Note: The subarray must contain at least one element. The array may contain both positive and negative integers.
The problem can be formally stated as follows:
Given an integer \( n \), an integer \( l \) and an array \( a_1, a_2, \dots, a_n \), find \[ \max_{1 \leq k \leq l} \; \max_{1 \leq i \leq n-k+1} \left( \sum_{j=i}^{i+k-1} a_j \right) \]
inputFormat
The first line contains two space-separated integers n and l, where n is the number of elements in the array and l is the maximum allowed subarray length.
The second line contains n space-separated integers representing the array.
outputFormat
Output a single integer representing the maximum sum of any contiguous subarray of length at most l.
## sample5 3
1 2 3 0 -1
6