#C9517. Maximum Contiguous Subarray Sum of Fixed Length

    ID: 53619 Type: Default 1000ms 256MiB

Maximum Contiguous Subarray Sum of Fixed Length

Maximum Contiguous Subarray Sum of Fixed Length

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

Formally, for an array \(a_1, a_2, \dots, a_n\), define the sum of a contiguous subarray starting at index \(i\) as \[ S(i) = \sum_{j=i}^{i+k-1} a_j \] for all \(1 \leq i \leq n-k+1\). Your goal is to compute the maximum value of \(S(i)\).

This problem can be efficiently solved using the sliding window technique, which allows you to compute each successive subarray sum in \(O(1)\) time after the initial sum is computed in \(O(k)\) time.

inputFormat

The first line contains two space-separated integers (n) and (k), where (n) is the number of elements in the array and (k) is the length of the subarray. The second line contains (n) space-separated integers representing the elements of the array.

outputFormat

Output a single integer representing the maximum sum of any contiguous subarray of length (k).## sample

8 3
1 2 5 2 8 1 5 2
15