n = int(input()) ls = [int(i) for i in input().split()] avg = sum(ls) // n for i in range(n): if i == 0: ls[i] -= avg else: ls[i] -= avg ls[i] += ls[i-1] ls.sort() tot = 0 for x in ls: tot += abs(x - ls[n // 2]) print(tot)