N=int(input()) A=sorted(map(int,input().split())) x=A[len(A)//2] ANS=0 for a in A: ANS+=abs(x-a) print(ANS)