import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from collections import deque N,*Y = map(int,read().split()) Y.sort() if Y[0] == Y[-1]: print(1) exit() def calc_left_sum(Y): n_left = 0 right = deque() s_l = 0 s_r = 0 ret = [10 ** 18] for y in Y: right.append(y) s_r += y if n_left + 1 < len(right): n_left += 1 x = right.popleft() s_l += x s_r -= x mid = right[0] D = (mid * n_left - s_l) D += (s_r - mid * len(right)) ret.append(D) return ret L = calc_left_sum(Y) R = calc_left_sum([-y for y in Y[::-1]]) answer = min(x+y for x,y in zip(L[1:N], R[1:N][::-1])) print(answer)