#C9015. Balancing the Magical Weights

    ID: 53062 Type: Default 1000ms 256MiB

Balancing the Magical Weights

Balancing the Magical Weights

In the mystical land of Balance Quest, you are tasked with positioning a magical balance rod among a set of mystical weights placed along a number line. The imbalance is defined as the sum of the absolute differences between the chosen rod position and each of the weights. To achieve equilibrium, you need to minimize this total imbalance.

The optimal strategy is to place the rod at the median of the positions. In the case where the number of weights n is even, choose the lower median, i.e., the element at index \(\frac{n}{2} - 1\) after sorting. Your job is to compute and output the minimum total imbalance.

inputFormat

The input is given via standard input and consists of two lines:

  • The first line contains a single integer \(n\) (\(1 \le n\)) representing the number of mystical weights.
  • The second line contains \(n\) space-separated integers denoting the positions of the weights on the number line.

outputFormat

Output a single integer which is the minimum total imbalance. That is, the sum of the absolute differences between each weight's position and the chosen median position (if \(n\) is even, use the lower median).

## sample
5
1 5 9 12 16
22

</p>