#K75952. Maximum Subarray Sum with at Least K Elements

    ID: 34533 Type: Default 1000ms 256MiB

Maximum Subarray Sum with at Least K Elements

Maximum Subarray Sum with at Least K Elements

Given a sequence of n integers and an integer k, your task is to find the maximum sum of any contiguous subarray whose length is at least k.

You can compute the prefix sums using the formula: \(P_i = \sum_{j=1}^{i} a_j\). Using these, the sum of the subarray from index i+1 to j is \(P_j - P_i\). By maintaining the minimum prefix sum for subarrays with the required length gap, you can efficiently compute the answer.

It is guaranteed that at least one subarray with the required length exists.

inputFormat

The input is given via standard input (stdin) and consists of two lines:

  • The first line contains two integers n and k, where 1 ≤ k ≤ n.
  • The second line contains n space-separated integers representing the array elements.

outputFormat

Print a single integer which is the maximum sum of any contiguous subarray of length at least k.

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