N = int(input()) Y = list(map(int,input().split())) Y.sort() sy = set(Y) if len(sy) == 1: print(1) exit() if len(sy) == 2: print(0) exit() ave = sum(Y)/N for i,a in enumerate(Y): if a > ave: mi = i break lm = mi//2 rm = mi + (N-mi)//2 ans = 0 for i in range(mi): ans += abs(Y[i] - Y[lm]) for i in range(mi,N): ans += abs(Y[i] - Y[rm]) print(ans)