#K706. Maximum Weight of K Consecutive Items

    ID: 33344 Type: Default 1000ms 256MiB

Maximum Weight of K Consecutive Items

Maximum Weight of K Consecutive Items

You are given an array of integers representing weights and an integer k. Your task is to find the maximum sum of any k consecutive weights in the array.

In other words, given an array \(w_1, w_2, \dots, w_n\) and an integer \(k\), compute:

[ \max_{1 \leq i \leq n-k+1} \sum_{j=i}^{i+k-1} w_j ]

You are encouraged to use an efficient sliding window technique to solve this problem within linear time complexity \(O(n)\).

inputFormat

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

  • The first line contains two integers \(n\) and \(k\) (1 \(\leq k \leq n\)), where \(n\) is the number of items and \(k\) is the number of consecutive items you must sum.
  • The second line contains \(n\) space-separated integers representing the weights.

outputFormat

Output the maximum sum of any k consecutive weights on a single line to stdout.

## sample
7 3
1 3 2 5 6 2 4
13