n = int(input()) Y = list(map(int, input().split())) Y.sort() x = Y[n // 2] ans = sum(abs(x - y) for y in Y) print(ans)