n = int(input()) Y = list(map(int, input().split())) Y.sort() import math if n%2 == 1: ans = 0 for i in range(n): ans += abs(Y[i]-Y[n//2]) else: y1 = math.floor((Y[n//2-1]+Y[n//2])/2) y2 = math.ceil((Y[n//2-1]+Y[n//2])/2) ans1 = 0 ans2 = 0 for i in range(n): ans1 += abs(Y[i]-y1) ans2 += abs(Y[i]-y2) ans = min(ans1, ans2) print(ans)