#K55157. Maximum Sum of Consecutive Elements

    ID: 29913 Type: Default 1000ms 256MiB

Maximum Sum of Consecutive Elements

Maximum Sum of Consecutive Elements

You are given several test cases. For each test case, you are provided with an integer \(k\) and an array of integers. Your task is to find the maximum possible sum of any \(k\) consecutive elements in the array. If the array has fewer than \(k\) elements, then the answer is \(0\). This problem requires you to apply a sliding window technique to calculate the sum efficiently.

Input Format:

  • The first line contains an integer \(T\), the number of test cases.
  • For each test case, the first line contains two integers \(k\) and \(n\), where \(k\) is the number of consecutive elements to sum, and \(n\) is the number of elements in the array.
  • The next line contains \(n\) space-separated integers constituting the array. If \(n = 0\), the line will be empty.

Output Format:

  • For each test case, output a single integer representing the maximum sum of \(k\) consecutive elements in the array. Print the result on a new line for each test case.

Note: If there are fewer than \(k\) elements in the array, output \(0\) for that test case.

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains an integer \(T\) denoting the number of test cases.
  • For each test case:
    • The first line contains two integers \(k\) and \(n\) separated by a space.
    • The second line contains \(n\) space-separated integers representing the array elements. If \(n = 0\), this line will be empty.

outputFormat

For each test case, output a single line with one integer: the maximum sum of \(k\) consecutive elements. If \(n < k\), output \(0\) for that test case.

## sample
6
3 5
1 2 3 4 5
2 3
1 3 4
4 3
1 2 3
1 0
2 1
10
1 1
10
12

7 0 0 0 10

</p>