import sys from functools import lru_cache N = int(input()) A = list(map(int, input().split())) if N < 3: print(0) sys.exit() A.sort() B = [0] * (N + 1) for i in range(N): B[i + 1] = B[i] + A[i] @lru_cache(maxsize=None) def g(x, y): return B[N] - B[N - y] + B[x] - B[x - y] - (2 * y) * A[x] @lru_cache(maxsize=None) def f(x): lb = 1 ub = min(x, N - x - 1) t1 = (ub + 2 * lb) // 3 t2 = -(-2 * ub - lb) // 3 while ub - lb > 2: # ??? a, b, c, d = g(x, lb), g(x, t1), g(x, t2), g(x, ub) if a <= b <= c: # ??? lb = t1 elif b >= c >= d: # ??? ub = t2 else: break t1 = (ub + 2 * lb) // 3 t2 = -(-2 * ub - lb) // 3 i += 1 return (max(g(x, lb), g(x, t1), g(x, t2), g(x, ub))) lb = 1 ub = N - 2 t1 = (ub + 2 * lb) // 3 t2 = -(-2 * ub - lb) // 3 i = 0 while ub - lb > 2: a, b, c, d = f(lb), f(t1), f(t2), f(ub) if a <= b <= c: lb = t1 elif b >= c >= d: ub = t2 else: break t1 = (ub + 2 * lb) // 3 t2 = -(-2 * ub - lb) // 3 #print(lb,t1,t2,ub) #print(f(lb),f(t1),f(t2),f(ub)) ans = max(f(lb), f(t1), f(t2), f(ub)) print(ans)