Cubes
InterviewTime limit2sMemory limit1024 MB
Given n tower heights, find the minimum number of move, add, or remove actions that make all towers the same height.
- Level
Easy3 of 10
- Topics
- Brute force, Implementation, Math
- Solved
- No attempts yet
Statement
One fine evening, Scrooge McDuck told his beloved grandchildren Billy, Dilly, and Willy another duck story and remembered how he loved playing with cubes as a child. Caught up in his memories, Scrooge suggested the kids build towers out of cubes. Everyone happily agreed.
The ducklings built towers in total. The -th tower has cubes stacked on top of each other. Scrooge noticed that the towers have different heights. He does not like that, since he is a great lover of order, and he decided to fix it. Scrooge will move, add, and remove cubes so that all towers end up the same height. In one action, Scrooge can move a cube from one tower to another, remove a cube from the construction, or take a cube from the supply and place it on some tower. The supply of cubes is unlimited. The height of a tower is the number of cubes in it.
Help Scrooge find the minimum number of actions needed to make all towers the same height.
Input
The first line contains the number of towers ().
The second line contains integers (), the number of cubes in the -th tower.
Output
Print a single number, the answer to the problem.