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