n = int(input()) a = sorted(list(map(int, input().split()))) med = a[n // 2] ans = sum(abs(med - i) for i in a) print(ans)