luogu#P4767. [IOI 2000] 邮局 加强版
[IOI 2000] 邮局 加强版
Problem Description
There are some villages along a highway. The highway is represented as the integer number line, and the position of each village is given by a single integer coordinate. No two villages are at the same position. The distance between two positions is the absolute value of the difference of their integer coordinates.
Post offices will be built in some, but not necessarily all, villages. To build the post offices, you should choose their locations so that the sum of distances from each village to its nearest post office is minimized.
You need to write a program that, given the positions of the villages and the number of post offices, computes the minimum possible total sum of distances from each village to its nearest post office.
Input Format
The first line contains two integers: the first is the number of villages , and the second is the number of post offices .
The second line contains integers. These integers are the positions of the villages.
Output Format
The first line contains one integer , which is the total sum of distances from each village to its nearest post office.
10 5
1 2 3 6 7 9 11 22 44 50
9
Hint
For of the testdata, .
For of the testdata, , , and village positions .
Translated by ChatGPT 5