N = int(input()) Y = list(map(int, input().split())) Y.sort() if N % 2 == 1: H = Y[(N + 1) // 2 - 1] print(sum([abs(Y[i] - H) for i in range(N)])) else: H1 = Y[N // 2 - 1] H2 = Y[N // 2] print(min(sum([abs(Y[i] - H1) for i in range(N)]), sum([abs(Y[i] - H2) for i in range(N)])))