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?
The first line contains integer *n* (1 ≤ *n* ≤ 50000). The second line contains *n* non-negative numbers that do not exceed 10<sup class="upper-index">9</sup>, 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*.
# cook your dish here
from itertools import accumulate
from bisect import bisect_left
n = int(input())
l = list(map(int, input().split()))

s = sum(l)
q = s // n

extras = []
extraind = []

lackee = []

for i, e in enumerate(l):
    
    if e > q:
        extras.append(e-q)
        extraind.append(i)
    elif e < q:
        lackee.append((i, q-e))

c = 0
i = 0
for indx, e in lackee:
    s = 0
    
    while s < e:
        # print(s, e)
        nextra = max(0, extras[i] - (e - s))
        # print('nextra', nextra, 'extra', extras[i])
        s += extras[i] - nextra 
        c += (extras[i] - nextra)*abs(indx-extraind[i])
        # print(c)
        extras[i] = nextra
        
        if extras[i] == 0:
            i += 1
print(c)
    
        

        
        
        
