#K76382. Equalize Array Elements

    ID: 34630 Type: Default 1000ms 256MiB

Equalize Array Elements

Equalize Array Elements

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

The optimal solution is to transform every element to the median of the array. The number of operations required is given by the formula:

[ \text{Operations} = \sum_{i=1}^{n} \left|a_i - m\right| ]

where \(m\) is the median of the array.

Example: For the array [1, 2, 3, 4, 5], the median is 3, and the total number of operations is \(|1-3|+|2-3|+|3-3|+|4-3|+|5-3| = 6\).

inputFormat

The first line contains an integer n (1 ≤ n ≤ 105), representing the number of elements in the array.

The second line contains n space-separated integers, each representing an element of the array. The absolute value of each integer does not exceed 109.

outputFormat

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

## sample
5
1 2 3 4 5
6