n = int(input()) y = [int(i) for i in input().split()] f = True for i in range(n-1): if y[i] != y[i+1]: f = False break if f == True: print(1) exit() y.sort() rui = [0]*(n+1) for i in range(n): rui[i+1] = rui[i] + y[i] ans = 10**15 for i in range(n-1): tmp = 0 idx = i+1 cidx = idx//2 bv = y[cidx] tmp += abs(bv*(cidx) - rui[cidx]) tmp += abs(bv*(idx-cidx) - (rui[idx] - rui[cidx])) #print(bv,tmp,cidx,idx,"bfr") cidx = (n-idx)//2+idx bv = y[cidx] tmp += abs(bv*(cidx-idx) - (rui[cidx]-rui[idx])) #print(bv*(cidx-idx+1), (rui[cidx+1]-rui[idx]),111) tmp += abs(bv*(n-cidx) - (rui[n] - rui[cidx])) #print(bv*(cidx-idx+1), (rui[n] - rui[cidx-1]),222) #print(bv,i,tmp,cidx,idx,"aft") ans = min(ans,tmp) #print(bv,i,tmp,cidx,idx) #print(bv,tmp,cidx,idx,abs(bv*(cidx-idx) - (rui[cidx]-rui[idx])),abs(bv*(n-cidx) - (rui[n] - rui[cidx])),"afr") print(ans)