#K52397. Minimizing Cost to Equalize Stacks
Minimizing Cost to Equalize Stacks
Minimizing Cost to Equalize Stacks
You are given n stacks, each with a certain height. In one move, you can transfer one unit from one stack to another. The goal is to equalize the heights of all stacks. Note that transferring a unit decreases the height of one stack by 1 and increases the height of another by 1.
Let \( H = h_1 + h_2 + \cdots + h_n \) be the total height of all stacks. Define the target height as \( m = \lfloor \frac{H}{n} \rfloor \). The minimum cost (number of moves) required is given by:
\[ \text{cost} = \frac{1}{2} \sum_{i=1}^{n} |h_i - m|. \]
Your task is to compute and output the minimum number of moves required to make all stacks equal to \( m \).
inputFormat
The input is given via standard input with the following format:
- The first line contains an integer \( n \) denoting the number of stacks.
- The second line contains \( n \) space-separated integers representing the heights \( h_1, h_2, \dots, h_n \) of the stacks.
outputFormat
Output via standard output a single integer representing the minimum number of moves required to equalize all stacks.
## sample4
1 2 3 4
2