#C3333. Minimum Operations to Equalize Elements

    ID: 46749 Type: Default 1000ms 256MiB

Minimum Operations to Equalize Elements

Minimum Operations to Equalize Elements

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

It can be proved that the optimal solution is achieved when all elements are transformed to the median of the array. In mathematical terms, if the array is a₁, a₂, ..., aₙ and m is the median, then the minimum operations required is (\sum_{i=1}^{n} |a_i - m|).

inputFormat

The first line contains a single integer n (1 ≤ n ≤ 10^5), representing the number of elements in the array. The second line contains n space-separated integers representing the elements of the array. ( -10^9 \leq a_i \leq 10^9 ).

outputFormat

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

4
2 2 3 3
2