atcoder#ABC218H. [ABC218H] Red and Blue Lamps

[ABC218H] Red and Blue Lamps

Score : 600600 points

Problem Statement

There are NN lamps numbered 11 through NN arranged in a row. You are going to light RR of them in red and NRN-R in blue.

For each i=1,,N1i=1,\ldots,N-1, a reward of AiA_i is given if Lamp ii and Lamp i+1i+1 light up in different colors.

Find the maximum total reward that can be obtained by efficiently deciding the colors of the lamps.

Constraints

  • 2N2×1052 \leq N \leq 2\times 10^5
  • 1RN11 \leq R \leq N-1
  • 1Ai1091 \leq A_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN RR

A1A_1 A2A_2 \ldots AN1A_{N-1}

Output

Print the answer.

6 2
3 1 4 1 5
11

Lighting up Lamps 3,53, 5 in red and Lamps 1,2,4,61, 2, 4, 6 in blue yields a total reward of A2+A3+A4+A5=11A_2+A_3+A_4+A_5=11.

You cannot get any more, so the answer is 1111.

7 6
2 7 1 8 2 8
10

Lighting up Lamps 1,2,3,4,5,71, 2, 3, 4, 5, 7 in red and Lamp 66 in blue yields a total reward of A5+A6=10A_5+A_6=10.

11 7
12345 678 90123 45678901 234567 89012 3456 78901 23456 7890
46207983