#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 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 (a positive integer), where . To improve cultural quality in the mountains, the government also decided to build primary schools in of the villages (assume ). Given 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 and , separated by a space.
The second line contains 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 schools among villages. The distance between village and village is , between village and village is , between village and village is , ..., and between village and village is .
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.