Under construction

Still being built. Things may change

Cubes

Interview

Time limit2sMemory limit1024 MB

Summary
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 nn towers in total. The ii-th tower has aia_i 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 nn (1≤n≤10001 \le n \le 1000).

The second line contains nn integers aia_i (1≤ai≤10001 \le a_i \le 1000), the number of cubes in the ii-th tower.

Output

Print a single number, the answer to the problem.

Examples1

  1. Example 1

    Input
    5
    3 2 2 5 4
    
    Expected output
    3