I=input n=int(I()) y=sorted(map(int,I().split())) m=(y[len(y)//2]+list(reversed(y))[n//2])//2 print(sum([abs(m-x) for x in y]))