#K52452. Maximize Sum of Differences in Robot Duels

    ID: 29312 Type: Default 1000ms 256MiB

Maximize Sum of Differences in Robot Duels

Maximize Sum of Differences in Robot Duels

You are given an even number of robots, each with a skill level. Your task is to pair these robots for duels such that in each duel one robot competes against another robot with an equal or higher skill level. When a robot with a lower skill level duels with one with a higher skill level, the difference in their skill levels is added to the total sum. Your objective is to maximize the overall sum of these differences.

More formally, let the sorted skills be \(S_0, S_1, \ldots, S_{N-1}\). The optimal sum is computed as follows:

\[ \sum_{i=0}^{\frac{N}{2}-1} \Big( S_{N-1-i} - S_{i} \Big) \]

It is guaranteed that the input number of robots N is even.

inputFormat

The input consists of two lines:

  1. The first line contains an even integer N, representing the number of robots.
  2. The second line contains N space-separated integers representing the skill levels of the robots.

outputFormat

Output a single integer which is the maximum sum of differences obtained by optimally pairing the robots.

## sample
4
1 3 5 9
10