#P1626. 象棋比赛

象棋比赛

Description

There are NN people participating in an international chess tournament, where KK games will be played. Each person can participate in at most two games and may also play zero games. Each person has a unique rating (represented by a positive integer, and all ratings are distinct).

In each game, the higher-rated player must use the black pieces, and the lower-rated player must use the white pieces. Each person can use the black pieces at most once and the white pieces at most once. To make the tournament more exciting, the audience hopes that the sum of rating differences between the two players across the KK games is minimized.

For example, there are 77 players with ratings 30,17,26,41,19,38,1830, 17, 26, 41, 19, 38, 18, and 33 games are to be played. The best arrangement is player 22 vs player 77, player 77 vs player 55, and player 66 vs player 44. In this case, the sum of rating differences equals (1817)+(1918)+(4138)=5(18-17)+(19-18)+(41-38)=5, which is minimal.

Input Format

The first line contains two positive integers N,KN, K.

The next NN lines follow. The ii-th line contains the rating of the ii-th person.

Output Format

On the first line, output the minimal possible sum of rating differences.

7 3
30
17
26
41
19
38
18
5

Hint

Constraints

  • In 90%90\% of the testdata, 1N30001 \le N \le 3000.
  • In 100%100\% of the testdata, 1N1000001 \le N \le 100000.
  • It is guaranteed that all ratings are less than 10910^9, and 1KN11 \le K \le N - 1.

Translated by ChatGPT 5