def calc(A, n): val = 0 for a in A: val += abs(A[n] - a) return val N = int(input()) Y = list(map(int, input().split())) Y.sort() if N % 2: print(calc(Y, N//2)) else: print(min(calc(Y, N//2), calc(Y, N//2 - 1)))