跳到主要内容
图灵 OJTURING / ONLINE JUDGE

#F1997. Balancer

    ID: 2003 传统题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 4 上传者: 标签>贪心模拟算法思想编程与模拟进阶难度共享题库Codeforces英文题面题目来源题面语言

Balancer

题目描述

B. Balancer
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration?

Input

The first line contains integer n (1≤n≤50000). The second line contains n non-negative numbers that do not exceed 109, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n.

Output

Print the total minimum number of moves.

Examples
Input
6
1 6 2 5 3 7
Output
12

题目来源:fps-www.educg.net-codeforce-1-2833.xml.zip;FPS 共享题包,题包内第 1584 题。保留原作者与原赛事署名。