n = int(input()) a = list(map(int, input().split())) a.sort() print(sum(abs(a[i] - a[len(a)//2]) for i in range(len(a))))
666
666