#K53782. Minimum Total Difference in Teams

    ID: 29608 Type: Default 1000ms 256MiB

Minimum Total Difference in Teams

Minimum Total Difference in Teams

You are given an integer M representing the number of players and a list of M integers representing the skill levels of each player. Your task is to form teams by pairing players such that the total difference in their skill levels is minimized.

To achieve the minimum total difference, you should follow these steps:

  1. Sort the list of skill levels in non-decreasing order.
  2. Form teams by pairing the first and second player, the third and fourth player, and so on.
  3. If the number of players is odd, the last player will remain unpaired and is not counted in the total difference.

The absolute difference for a pair of players with skills x and y is given by the formula: \( |x - y| \).

Your program should then output the sum of the absolute differences for all the pairs formed.

Note: The input is read from standard input (stdin) and the output is written to standard output (stdout).

inputFormat

The first line of input contains a single integer M which is the number of players. The second line contains M space-separated integers representing the skill levels of the players.

For example:

5
10 20 30 40 50

outputFormat

Output a single integer which is the minimum total difference in skill levels across all the teams formed.

For example, for the input above, the output should be:

20

In this example, after sorting (which keeps the list the same) the teams formed are:

  • Team 1: 10 and 20, difference = \(20-10=10\)
  • Team 2: 30 and 40, difference = \(40-30=10\)

The last player (50) is left unpaired. Thus, the total minimum difference is \(10+10=20\).

## sample
4
1 3 5 7
4

</p>