luogu#P4998. 信号站

信号站

Background

A poverty alleviation program has arrived at Village Q. The team plans to build signal stations in Village Q so that it is no longer “isolated from the outside world”, and so that people can get rich information from outside.

Problem Description

Village Q is extremely poor, and the whole village has only one road. Along this road, there are nn households. Due to limited conditions, there may be multiple households at the same point. Because transportation in the mountains is underdeveloped, the team can only build kk signal stations, and they want the sum of the “unreasonableness values” of all stations to be as small as possible. The unreasonableness value of a signal station is defined as the sum of its distances to every household.

Distance is calculated as follows: if a signal station is at coordinate xx and a household is at coordinate yy, then the distance between them is xy|x-y|.

The team is good at building signal stations, but not good at choosing locations (because they are not good at math QwQ). They hope you, a programming expert, can help them choose the best locations for the signal stations, so that the sum of unreasonableness values of the kk signal stations is minimized.

The testdata guarantees that the number of households is greater than the number of signal stations. The coordinates where signal stations are placed must be integers, and at most one signal station can be placed at the same coordinate.

Input Format

There are two lines of input.

The first line contains two integers n,kn, k separated by spaces, representing the number of households and the number of signal stations.

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n separated by spaces, representing the coordinates of each household.

Output Format

Output one line containing one integer, representing the minimum possible sum of unreasonableness values.

7 2
1 1 2 2 3 3 4
13

Hint

Sample Explanation

Place one signal station each at coordinates 22 and 33. The total unreasonableness value is 6+7=136 + 7 = 13. It can be proven that there is no better solution.

Constraints

For 70%70\% of the testdata, 1k<n1031 \le k < n \le 10^3.

For 100%100\% of the testdata, 1k<n1061 \le k < n \le 10^6, 0ai1060 \le a_i \le 10^6.

Translated by ChatGPT 5