ÀÂÒ
Language:

Remote Training on Programming

Problems On-line status Contests FAQ
For authors:
Register  ||  Login
 
Hello, Guest! Login or register.

887. Beads game

Time Limit: 1 seconds
Memory Limit:32768KB
Points:10
View Problem Statistics Submit Problem added Administrator

There are n boxes in a row. Each box contains Ki beads. It is known that the total number of beads is a multiple of n. On each move a single bead can be moved from its box to an adjacent box (to the left or to the right). Obviously, there exists a sequence of moves that will result in the same number of beads in each box.

Write a program to determine the minimum number of moves leading to equal number of beads in all boxes.

Limitations

1 <= n <= 100 000;            0 <= Ki <= 100 000.

Input

The first line contains an integer n. The following line contain numbers Ki.

Output

The output file must specify a single integer, the minimum possible number of moves leading to equal number of beads in all boxes.

Sample

Standard input

Standard output

5

4 1 3 2 5

5

 


View Problem Statistics Submit Author/source:
Problems from Contests / ACM Contests / Rybinsk-2010 /
886. A - Spiral 887. 888. C - Numbers 889. D - Bubble 890. E - Maze
We can all benefit by doing occasional "toy" programs, when artificial restrictions are set up, so that we are forced to push our abilities to the limit. The art of tackling miniproblems with all our energy will sharpen our talents for the real problems. Donald E. Knuth.
time generating 0.063 sec.
© Copyright VSTU, AVT, Nosov D.A.