N = int(input()) A = sorted(list(map(int,input().split()))) S = [0 for k in range(N)] S[0] = A[0] for k in range(1,N): S[k] = S[k-1] + A[k] B = A[::-1] T = [0 for k in range(N)] T[0] = B[0] for k in range(1,N): T[k] = T[k-1] + B[k] ans = -float("inf") for k in range(1,N-1): ans = max(ans,-k*A[k]+S[k-1]+T[k-1]-k*A[k]) print(ans)