N=int(input()) A = list(map(int,input().split())) A.sort() mid = A[N//2] ans = 0 for a in A: ans += abs(a-mid) print(ans)