#P4677. 山区建小学

山区建小学

Description

The government has built a single road in a mountainous area that passes through each of the nn villages exactly once, with no cycles or intersections. Any two villages can communicate only along this road. The distance between any two adjacent villages is did_i (each did_i is a positive integer), where 1i<n1 \le i < n. To improve education in the area, the government will choose mm out of the nn villages to build primary schools. Given nn, mm, and the distances between all adjacent villages, decide in which villages to build the primary schools so that the sum of distances from all villages to their nearest primary school is minimized, and compute this minimum value.

Input Format

The first line contains nn and mm, separated by a space. The second line contains n1n-1 integers, in order from one end to the other, representing the distances between adjacent villages, separated by spaces.

For example

10 3
2 4 6 5 2 4 3 1 3

means building 33 primary 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

Output the minimal sum of distances from all villages to their nearest primary school.

10 2
3 1 3 1 1 1 1 1 3
18

Hint

1mn<5001 \le m \le n < 500, 1di1001 \le d_i \le 100.

Translated by ChatGPT 5