N = int(input()) a = list(map(int, input().split())) a.sort() b = [0]*(N+1) for i in range(1,N+1): b[i] += b[i-1]+a[i-1] res = 0 for i in range(1,N-1): ok,ng = 0,min(i,N-1-i)+1 while ng-ok>1: mid = (ok+ng)//2 if b[i]-b[i-mid]+b[N]-b[N-mid]-2*mid*a[i]>0: ok = mid else: ng = mid res = max(res,b[i]-b[i-ok]+b[N]-b[N-ok]-2*ok*a[i]) print(res)