#K67857. Equalizing Array Elements

    ID: 32735 Type: Default 1000ms 256MiB

Equalizing Array Elements

Equalizing Array Elements

You are given an array of integers. In one operation, you can either increase or decrease an element by 1. The goal is to make all elements equal with a minimum number of such operations.

The optimal solution is to adjust every element to the median of the array. Mathematically, if you denote the array as \(a_1, a_2, \dots, a_n\) and choose the median value \(m\), then the minimum number of operations required is:

\(\sum_{i=1}^{n} |a_i - m|\)

This problem tests your understanding of median properties and absolute deviation minimization.

inputFormat

The input is given via standard input (stdin) and consists of two lines:

  1. The first line contains a single integer \(n\), representing the number of elements in the array.
  2. The second line contains \(n\) space-separated integers representing the array elements.

outputFormat

Output to standard output (stdout) a single integer representing the minimum number of operations required to make all array elements equal.

## sample
3
1 2 3
2