#t318. 山区建小学

山区建小学

Building Primary Schools in the Mountains

Problem Background

Special for beginners, ^_^

Problem Description

The government built a road in a mountainous area that passes through each of mm villages exactly once, with no loops or crossings, so any two villages can only reach each other via this road. The distance between any two adjacent villages is did_i (a positive integer), where 0<i<m0 < i < m. To improve cultural quality in the mountains, the government also decided to build primary schools in nn of the mm villages (assume 0<n≤m<5000 < n \le m < 500). Given m,nm, n and the distances between all adjacent villages, choose which villages to build schools in so that the sum of distances from every village to its nearest school is minimized, and compute that minimum value.

Input Format

The first line contains mm and nn, separated by a space.

The second line contains m−1m - 1 integers, giving the distances between adjacent villages from one end to the other, separated by spaces.

For example:

10 3
2 4 6 5 2 4 3 1 3

means building 33 schools among 1010 villages. The distance between village 11 and village 22 is 22, between village 22 and village 33 is 44, between village 33 and village 44 is 66, ..., and between village 99 and village 1010 is 33.

Output Format

The minimum sum of distances from every village to its nearest school.

Sample Input

10 2
3 1 3 1 1 1 1 1 3

Sample Output

18

Constraints

1s, 1024KiB for each test case.