#K48487. Minimum Moves to Equal Array Elements

    ID: 28431 Type: Default 1000ms 256MiB

Minimum Moves to Equal Array Elements

Minimum Moves to Equal Array Elements

You are given an array of n integers. Your task is to compute the minimum number of moves required so that all elements become equal. In one move, you can increment or decrement any element by exactly 1.

Hint: To minimize the total number of moves, consider aligning all elements to the median value of the array. More formally, if the sorted array is \(a_1, a_2, \dots, a_n\), you will minimize the sum \(\sum_{i=1}^{n} |a_i - m|\), where \(m\) is chosen as the median of the array.

The input is read from standard input and the output should be printed to standard output.

inputFormat

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

outputFormat

Output a single integer representing the minimum number of moves required so that all elements of the array become equal.

## sample
4
1 2 3 4
4

</p>