#C13947. Maximum Sum of k-Consecutive Elements
Maximum Sum of k-Consecutive Elements
Maximum Sum of k-Consecutive Elements
You are given an array of integers and an integer k. Your task is to find the maximum sum of any k consecutive elements (i.e. a contiguous subarray of length k) in the array.
If \( k > n \) (where \( n \) is the length of the array) or if \( k \le 0 \), then the output should be 0.
Example:
For the array [1, 2, 3, 4, 5, 6, 7, 8, 9] and \( k = 3 \), the maximum sum is obtained from the subarray [7, 8, 9], and the answer is 24.
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains two integers \( n \) and \( k \), separated by a space, where \( n \) is the number of elements in the array.
- The second line contains \( n \) integers separated by spaces, representing the elements of the array.
outputFormat
The output is the maximum sum of any k consecutive elements in the array, printed to standard output (stdout). If \( k > n \) or \( k \le 0 \), print 0.
## sample9 3
1 2 3 4 5 6 7 8 9
24