N = int(input()) Y = list(map(int,input().split())) Y.sort() s = set(Y) import sys if len(s) == 1: print(1) exit() _sum = [0] * (N+1) for i in range(N): _sum[i+1] = _sum[i] + Y[i] ans = 10 ** 18 def func(l,r): mid = (l+r) // 2 ans = _sum[r+1] - _sum[mid] - Y[mid] * (r-mid+1) ans += Y[mid] * (mid - l) - (_sum[mid] - _sum[l]) return ans for i in range(N-1): tmp = func(0,i) + func(i+1,N-1) if tmp < ans: ans = tmp print(ans)