#C4368. Maximum Sliding Window Sum

    ID: 47898 Type: Default 1000ms 256MiB

Maximum Sliding Window Sum

Maximum Sliding Window Sum

You are given an array (or sequence) of integers and a window size \(k\). Your task is to compute the maximum sum over all continuous subarrays (or windows) of size \(k\). In other words, given a sequence \(a_1, a_2, \dots, a_n\), find the maximum value of

[ S(i) = \sum_{j=i}^{i+k-1} a_j, \quad 1 \leq i \leq n-k+1 ]

where \(n\) is the number of elements in the array and \(k\) is the window size.

Note: The sequence may contain negative numbers and/or repeated numbers.

inputFormat

The input is given via standard input with the following format:

  • The first line contains two integers \(n\) and \(k\) where \(n\) is the number of elements in the sequence and \(k\) is the size of the sliding window.
  • The second line contains \(n\) space-separated integers representing the sequence.

outputFormat

Output a single integer, which is the maximum sum obtained among all contiguous subarrays of size \(k\).

## sample
8 3
1 3 -1 -3 5 3 6 7
16