Circular Barn

Choose up to k outer doors on a ring of n rooms so the total clockwise walking distance to every cow room is minimized.

Medium7Dynamic programmingPrefix sumNo attempts yetTime limit2sMemory limit512 MB

Problem

Farmer John likes contemporary architecture, so his new barn is a perfect circle. Inside, the barn is a ring of nn rooms, numbered 11 through nn clockwise around the perimeter (3n10003 \le n \le 1000). Each room has a door to each of its two neighbouring rooms, and one more door that opens to the outside.

Farmer John wants exactly rir_i cows to end up in room ii (1ri10000001 \le r_i \le 1000000). To herd the cows in without a stampede he unlocks at most kk of the exterior doors (1k71 \le k \le 7), and a cow may enter only through an unlocked door. Once inside, a cow walks clockwise from room to room until she reaches her own room. Walking between two neighbouring rooms adds 11 to the distance. The cows may line up outside the unlocked doors however they like, and lining up costs no distance.

Find the smallest total distance the cows walk after entering the barn, over the best choice of doors.

Input

The first line contains nn and kk. Each of the next nn lines contains one value, r1r_1 through rnr_n in order.

Output

Print the minimum total distance the cows walk.

Hint

In the first example Farmer John unlocks doors 22 and 55. The 1111 cows entering at door 22 walk a total distance of 88 to reach rooms 22, 33 and 44. The 1010 cows entering at door 55 walk a total distance of 66 to reach rooms 55, 66 and 11.