from itertools import accumulate N = int(input()) Y = [int(i) for i in input().split()] Y.sort() A = [0] + list(accumulate(Y)) def solve(l, r): m = l + (r - l) // 2 a = Y[m] * (m - l + 1) - A[m + 1] + A[l] b = A[r] - A[m + 1] - (r - 1 - (m + 1) + 1) * Y[m] return (m, a + b) mi = 1e20 for i in range(1, N): m1, a = solve(0, i) m2, b = solve(i, N) mi = min(mi, 1 if Y[m1] == Y[m2] else a + b) print(mi)