#C10346. Maximum Contiguous Subarray Sum with Length Constraint

    ID: 39541 Type: Default 1000ms 256MiB

Maximum Contiguous Subarray Sum with Length Constraint

Maximum Contiguous Subarray Sum with Length Constraint

You are given T test cases. In each test case, you are provided with an array of N integers and an integer L representing the maximum allowed length of a contiguous subarray. Your task is to determine the maximum possible sum obtainable by any contiguous subarray whose length is at most L.

In mathematical notation, for each test case, you need to compute:

[ \max_{0 \le i < N} \left{ \sum_{j=i}^{\min(i+L-1, N-1)} a_j \right} ]

where \(a_0, a_1, \dots, a_{N-1}\) is the given array. Note that the subarray must contain at least one element. If all numbers are negative, the answer is the maximum (least negative) element.

Please note that the input is received from standard input (stdin) and the output should be printed to standard output (stdout).

inputFormat

The input begins with an integer T (number of test cases). Each test case is described as follows:

  • The first line of each test case contains two integers N and L, where N is the number of elements in the array and L is the maximum length allowed for the subarray.
  • The second line contains N integers representing the elements of the array.

All input is read from stdin.

outputFormat

For each test case, output one line containing a single integer: the maximum subarray sum for a contiguous subarray with length at most L.

All output should be written to stdout.

## sample
2
5 3
1 -2 3 4 -1
4 2
-1 -2 3 4
7

7

</p>