#C1077. Equalizing Block Heights

    ID: 40011 Type: Default 1000ms 256MiB

Equalizing Block Heights

Equalizing Block Heights

You are given n blocks with integer heights. In one operation, you can either increase the height of one of the blocks that currently has the minimum height by 1, or decrease the height of one of the blocks that currently has the maximum height by 1.

Your task is to determine the minimum number of operations required to make all block heights equal.

Let \( h_{max} = \max\{h_1, h_2, \ldots, h_n\} \) and \( h_{min} = \min\{h_1, h_2, \ldots, h_n\} \). It can be shown that the minimum number of operations needed is:

\[ \text{operations} = h_{max} - h_{min} \]

Note that when there is only one block, no operations are needed.

inputFormat

The first line contains a single integer n (1 ≤ n ≤ 105), representing the number of blocks.

The second line contains n space-separated integers, where the i-th integer represents the height of the i-th block. Each height is between 1 and 105.

outputFormat

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

## sample
3
5 2 7
5