#C2470. Maximum Apples Collection

    ID: 45790 Type: Default 1000ms 256MiB

Maximum Apples Collection

Maximum Apples Collection

You are given a sequence of n integers where each integer represents the number of apples produced by a tree in a row. You are also given an integer K. Your task is to determine the maximum number of apples that can be collected from any contiguous segment of exactly K trees.

Formally, if the array of apple yields is denoted as \(apples[0 \ldots n-1]\), you need to compute:

\(\max_{0 \leq i \leq n-K} \left( \sum_{j=i}^{i+K-1} apples[j]\right)\)

If K is less than or equal to 0, or if there are no trees, the answer is defined to be 0. Similarly, if K is greater than n, no valid segment exists and the output should be 0.

inputFormat

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

  • The first line contains two space-separated integers n and K, where n is the number of trees and K is the length of the contiguous segment to consider.
  • The second line contains n space-separated integers representing the number of apples on each tree.

outputFormat

Output a single integer via standard output (stdout), which is the maximum sum of apples from any contiguous segment of exactly K trees. In case no valid segment exists, output 0.

## sample
7 3
1 3 4 2 5 8 2
15