N = int(input()) A = sorted(list(map(int, input().split()))) Ac = [0] * (N + 1) for i in range(N): Ac[i + 1] = Ac[i] + A[i] inf = 10 ** 18 ans = inf for i in range(N-1): m1 = i//2 m2 = i + (N - i)//2 if A[m1] == A[m2]: continue v1 = Ac[i + 1] - Ac[m1 + 1] * 2 + A[m1] * (1 - i%2) v2 = Ac[N] - Ac[m2 + 1] * 2 + Ac[i + 1] + A[m2] * (1- (N - i)%2) # print("test",i, m1, m2, v1, v2) ans = min(ans, v1 + v2) print(ans) if ans != inf else print(1)