#C10801. Maximum Flower Sum
Maximum Flower Sum
Maximum Flower Sum
You are given several test cases. In each test case, you are provided with n flowers arranged in a row, each having a magical value, and an integer k representing the size of a contiguous segment.
Your task is to compute the maximum possible sum of the magical values for any contiguous segment of exactly k flowers. The test cases are given one after another and the input terminates with a line containing 0 0
, which should not be processed.
Note: The solution is expected to use an efficient algorithm (e.g., sliding window) in order to handle larger inputs.
The formula for the sum of a segment starting at index i is given by:
[ S = \sum_{j=i}^{i+k-1}{a_j} ]
where \(a_j\) is the magical value of the j-th flower.
inputFormat
The input consists of multiple test cases. Each test case includes:
- A line with two integers
n
andk
, wheren
is the number of flowers andk
is the segment size. - A line with
n
space-separated integers representing the magical values of the flowers.
The input terminates with a test case where n = 0
and k = 0
(this test case should not be processed).
The input is read from standard input (stdin).
outputFormat
For each test case, output a single line containing the maximum sum of any contiguous segment of exactly k
flowers. The output for each test case should be printed to standard output (stdout).
5 3
1 -2 3 4 -1
6 2
-1 2 3 -2 5 1
0 0
6
6
</p>