All problems

Problem 719

Maximum Points You Can Obtain from Cards

Medium · 20 pts · Array, Sliding Window, Prefix Sum

Problem

Cards lie in a row, each worth some points. In each of exactly K steps you take one card from either the left end or the right end of the row. Maximise the total points of the K cards you take. Whatever you do, you take some L cards from the left and K - L from the right, so try every L from 0 to K, sliding one card at a time from the left part to the right part and updating the sum in O(1).

Input

The first line contains N and K. The second line contains the N card values.

Output

Print the maximum total points.

Constraints

1 <= K <= N <= 100000; 1 <= value <= 10^4

Example input

7 3
1 2 3 4 5 6 1

Example output

12

Explanation

For cards 1 2 3 4 5 6 1 with K = 3, taking the three rightmost cards 5, 6 and 1 gives 12, more than any other choice. So the answer is 12.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Number of Greater Elements to the Right Next: Number of Substrings Containing All Three Characters