n = int(input()) *a, = map(int,input().split()) a.sort() v = a[n//2] print(sum(abs(ai-v) for ai in a))