#C1315. Minimum Operations to Equalize Array Elements

    ID: 42656 Type: Default 1000ms 256MiB

Minimum Operations to Equalize Array Elements

Minimum Operations to Equalize Array Elements

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

The optimal strategy is to make all elements equal to the median of the array. More formally, if the sorted array is \(a_1 \le a_2 \le \cdots \le a_n\) then the answer is \(\sum_{i=1}^{n} |a_i - a_{\lfloor n/2 \rfloor + 1}|\).

inputFormat

The first line of input contains a single integer n, denoting the number of elements in the array.

The second line contains n space-separated integers representing the array.

outputFormat

Output a single integer, which is the minimum number of operations required to make all elements in the array equal.

## sample
3
1 2 3
2