#K33212. Maximum Sum of K Consecutive Elements

    ID: 25038 Type: Default 1000ms 256MiB

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 compute the maximum sum of any k consecutive elements in the array. If the array contains fewer than k elements, output 0.

This problem can be formulated mathematically as finding $$\max_{0 \leq i \leq n-k}\;\sum_{j=i}^{i+k-1} a_j,$$ where \(n\) is the total number of elements. A sliding window approach provides an efficient solution with linear time complexity.

inputFormat

The input is read from stdin and consists of the following parts:

  1. An integer n representing the number of elements in the array.
  2. n space-separated integers representing the array elements.
  3. An integer k representing the number of consecutive elements to consider.

outputFormat

Output a single integer to stdout representing the maximum sum of any k consecutive elements in the array. If the array has fewer than k elements, output 0.

## sample
5
1 2 3 4 5
2
9

</p>