N = int(input()) Y = [int(y) for y in input().split()] Y.sort() x = Y[N//2] ans = 0 for y in Y: ans += abs(y-x) print(ans)