#P5929. [POI 1999 R3] 地图

[POI 1999 R3] 地图

Background

A population statistics office needs to draw a map.

Problem Description

Due to technical reasons, only a small number of colors can be used. Two regions with the same or similar populations should use the same color on the map. For a color kk, let A(k)A(k) be the corresponding value, then:

  • Among the regions colored with color kk, at least half of them have population not greater than A(k)A(k);
  • Among the regions colored with color kk, at least half of them have population not less than A(k)A(k).

The coloring error of a region is the absolute value of the difference between its population and A(k)A(k). The total error is the sum of the coloring errors of all regions. We need to find an optimal coloring plan that minimizes the total error.

Input Format

The first line contains an integer nn, the number of regions.

The second line contains an integer mm, the number of colors.

Each of the next nn lines contains a non-negative integer, the population of a region.

All populations are at most 2302^{30}.

Output Format

Output one integer, the minimum total error.

11
3
21
14
6
18
10
2
15
12
3
2
2
15

Hint

For 100%100\% of the testdata, 10<n<300010 < n < 3000, 2m102 \le m \le 10.

Translated by ChatGPT 5