N = int(input()) Y = [int(y) for y in input().split()] Y.sort() if N%4 <= 1: a = Y[(N-1)//4] b = Y[3*(N-1)//4] else: a = Y[(N-1)//4] b = Y[3*(N-1)//4+1] ans = 0 for y in Y: ans += min(abs(y-a), abs(y-b)) if Y[0] == Y[-1]: ans = 1 print(ans)