#C3337. Maximum Sum of Contiguous Subarray of Fixed Length

    ID: 46753 Type: Default 1000ms 256MiB

Maximum Sum of Contiguous Subarray of Fixed Length

Maximum Sum of Contiguous Subarray of Fixed Length

You are given an array of integers of length \(n\) and an integer \(k\). Your task is to find the maximum sum of any contiguous subarray of length exactly \(k\).

In formal terms, given an array \(a_0, a_1, \ldots, a_{n-1}\), you need to find: \[ \max_{0 \leq i \leq n-k} \left(\sum_{j=i}^{i+k-1} a_j\right) \]

This problem can be efficiently solved using a sliding window technique, where you first compute the sum of the first \(k\) elements and then slide the window through the array, updating the sum with each step.

inputFormat

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

  1. The first line contains two space-separated integers \(n\) (the number of elements) and \(k\) (the size of the subarray).
  2. The second line contains \(n\) space-separated integers representing the elements of the array.

outputFormat

Output a single integer to standard output (stdout) representing the maximum sum of any contiguous subarray of exactly length \(k\).

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