#K15531. Minimum Operations to Equalize Array Elements

    ID: 24377 Type: Default 1000ms 256MiB

Minimum Operations to Equalize Array Elements

Minimum Operations to Equalize Array Elements

You are given an array of n integers. In one operation, you can increment or decrement any element by 1. The task is to compute the minimum number of operations required to make all array elements equal.

The optimal strategy is to change all numbers to the median of the array. Recall that the median minimizes the sum of absolute deviations. That is, if we denote the array by \(x_1, x_2, \dots, x_n\) and choose a target value \(m\), then the total number of operations needed is $$\sum_{i=1}^{n} |x_i - m|.$$ When \(m\) is chosen as the median, this sum is minimized.

Note: The input is provided via standard input (stdin) and the output should be printed to standard output (stdout).

inputFormat

The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)), the number of elements in the array. The second line contains \(n\) space-separated integers \(x_1, x_2, \dots, x_n\) (\(|x_i| \leq 10^9\)), representing the elements of the array.

outputFormat

Output a single integer representing the minimum number of operations required to make all elements equal.

## sample
5
1 2 3 4 5
6