#K45612. Max Sum of K Consecutive Elements

    ID: 27793 Type: Default 1000ms 256MiB

Max Sum of K Consecutive Elements

Max Sum of K Consecutive Elements

Given a list of integers and an integer \( k \), find the maximum sum of any \( k \) consecutive elements in the list. If \( k \) is larger than the length of the list, output \(-1\). You can use the sliding window technique to achieve an optimal solution.

For a list \( a_1, a_2, \dots, a_n \), the sum for a window starting at index \( i \) (1-indexed) is given by:

\[ S_i = \sum_{j=i}^{i+k-1} a_j \]

Your task is to calculate the maximum \( S_i \) for all possible \( i \), or output \(-1\) if \( k > n \).

inputFormat

The input is read from standard input (stdin) and consists of two lines. The first line contains two integers \( n \) and \( k \), where \( n \) is the number of elements in the list, and \( k \) is the number of consecutive elements to consider.

The second line contains \( n \) space-separated integers representing the list for which the maximum sum of \( k \) consecutive elements is to be found.

outputFormat

Output a single line to standard output (stdout) containing one integer: the maximum sum of any \( k \) consecutive elements. If \( k \) is greater than \( n \), output \(-1\).

## sample
6 3
2 1 5 1 3 2
9