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