PDP-24 (2012) - Camp common - 1 (maxksum)

View as PDF

Submit solution

Points: 30 (partial)
Time limit: 1.0s
Memory limit: 16M

Author:
Problem types
Allowed languages
C, C++, Java, Pascal, Python

MAXKSUM

We are given a sequence of N integers (negative, positive, or zero) and a natural number K, 1 \le K \le N. We are asked to find the maximum value of the sum of K or fewer consecutive terms of the sequence.

Input

The first line of input will contain two integers N and K, separated by a single space. The second line will contain N integers, separated in pairs by single spaces, representing the sequence.

Output

The output should consist of a single line containing exactly one integer: the maximum value of the sum of K or fewer consecutive terms of the sequence.

Constraints

2 \le N \le 1.000.000.
1 \le K \le N.
The numbers in the sequence will have an absolute value at most equal to 30.000.
Execution time limit: 1 sec.
Maximum available memory: 16 MB.

Examples of Input - Output
1st

STDIN (maxksum.in)

10 6
1 10 -1 -1 4 -8 7 2 -1 4

STDOUT (maxksum.out)

13

2nd

STDIN (maxksum.in)

10 4
1 7 -10 -11 9 9 -7 2 12 -4

STDOUT (maxksum.out)

18

3d

STDIN (maxksum.in)

4 2
-1 -1 -1 -1

STDOUT (maxksum.out)

0

Explanation

In the examples, the terms of the sequence that contribute to the maximum sum are highlighted in bold. In the third example, the maximum sum is achieved by not summing any term of the sequence, as they are all negative.


Comments

There are no comments at the moment.