import sys N=int(input()) A=sorted(map(int,input().split())) from itertools import accumulate S=[0]+list(accumulate(A)) if N<=2: print(0) sys.exit() ANS=0 for i in range(1,N-1): OK=0 NG=i+1 while NG-OK>1: mid=(OK+NG)//2 if mid>=1 and A[-mid]-A[i]>A[i]-A[i-mid]: OK=mid else: NG=mid if OK==0: continue #print(i,OK,S[-1]-S[-OK-1]+S[i+1]-S[i-OK]-A[i]*(2*OK+1)) ANS=max(ANS,S[-1]-S[-OK-1]+S[i+1]-S[i-OK]-A[i]*(2*OK+1)) print(ANS)