import collections N = int(input()) A = list(map(int,input().split())) AC = collections.Counter(A) if len(AC)==1: print(1) exit() A.sort() ME = A[N//2] FH=[] SH=[] for i in range(N): if ME>=A[i]: FH.append(A[i]) else: SH.append(A[i]) ans = 0 FM = FH[len(FH)//2] SM = SH[len(SH)//2] for f in FH: ans += abs(f-FM) for s in SH: ans += abs(s-SM) print(ans)