#C1738. Minimum Tournament Score

    ID: 44976 Type: Default 1000ms 256MiB

Minimum Tournament Score

Minimum Tournament Score

You are given n players with their respective skill levels. You need to design a tournament such that all players are connected (i.e. every player can be reached via selected matches) while minimizing the total score of the tournament. The score for a match between player i and player j is given by the absolute difference of their skill levels, i.e., \(|skill_i - skill_j|\).

This problem can be transformed into finding a minimum spanning tree (MST) on a graph where each player is a node and the weight of an edge between any two players is \(|skill_i - skill_j|\). A known observation is that when the players' skills are sorted in non-decreasing order \(s_1, s_2, \ldots, s_n\), the MST can be constructed by connecting consecutive players. Thus, the minimum total score is given by:

\( \text{Answer} = \sum_{i=2}^{n} (s_i - s_{i-1}) \)

Compute and output this minimal total score.

inputFormat

The input consists of two lines:

  • The first line contains an integer n (the number of players).
  • The second line contains n space-separated integers representing the skill levels of the players.

outputFormat

Output a single integer representing the minimum possible total score of the tournament.

## sample
4
5 3 9 1
8