#P6246. [IOI 2000] 邮局 加强版 加强版

    ID: 5244 远端评测题 2000~6000ms 250MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2000二分IOIO2优化凸完全单调性,凸单调

[IOI 2000] 邮局 加强版 加强版

Description

There are nn villages along a highway. The highway is represented as an integer line, and the position of each village is given by a single integer coordinate. The distance between two positions is the absolute value of the difference of their integer coordinates.

Now we want to build mm post offices. Post offices will be built in some, but not necessarily all, villages. To build the post offices, we 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 village positions 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, representing the number of villages nn and the number of post offices mm.

The second line contains nn integers, where the ii-th integer aia_i is the coordinate of the ii-th village.

Output Format

Output one line with one integer, representing the answer.

5 2
0 1 2 3 4
3

Hint

Constraints

This problem has five test points. The information for each test point is as follows:

Test Point ID n=n = aia_i \leq
1 5000050000 6×1046 \times 10^4
2 150000150000 2×1052 \times 10^5
3 299998299998 5×1055 \times 10^5
4 499998499998 10610^6
5 499999499999 2×1062\times 10^6

For all test points, it is guaranteed that 1mn5×1051 \leq m \leq n \leq 5 \times 10^5, 0ai2×1060 \leq a_i \leq 2\times 10^6, and the values of aia_i are uniformly random within the corresponding range.

It is guaranteed that the final answer does not exceed 10910^9.

Translated by ChatGPT 5