from itertools import accumulate def sum_dist(A, l, r): if (r - l) % 2 == 0: right = A[r] - A[(l+r)//2] left = A[(l+r)//2] - A[l] return - left + right else: right = A[r] - A[(l+r)//2 + 1] left = A[(l+r)//2] - A[l] return - left + right def main(): N = int(input()) Y = list(map(int, input().split())) sort_Y = sorted(Y) if len(set(Y)) == 1: print(1) exit() elif len(set(Y)) == 2: print(0) exit() A = [0] + list(accumulate(sort_Y)) ans = float('inf') for i in range(N): tmp = sum_dist(A,0,i) + sum_dist(A,i,N) ans = min(ans, tmp) print(ans) if __name__ == "__main__": main()