MAXKSUM
We are given a sequence of integers (negative, positive, or zero) and a natural number , . We are asked to find the maximum value of the sum of or fewer consecutive terms of the sequence.
Input
The first line of input will contain two integers and , separated by a single space. The second line will contain 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 or fewer consecutive terms of the sequence.
Constraints
.
.
The numbers in the sequence will have an absolute value at most equal to .
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