LogiQuest

421. Maximum Points From Cards

MediumSliding WindowPrefix Sum

Take exactly k cards, each time from the left or right end of the row. Print the maximum total.

Input format

Line 1: n k Line 2: n card values

Output format

The maximum total.

Example 1

Input

7 3
1 2 3 4 5 6 1

Output

12

Example 2

Input

3 2
2 2 2

Output

4

Constraints

1 ≤ k ≤ n ≤ 10⁵

Read input from standard input and print the answer to standard output. Your code is judged against 4 test cases, most of them hidden.

Loading the MyLoginn IDE…