N = int(input()) a = list(map(int,input().split())) a.sort(reverse = True) ans = 0 import sys if N <= 2: print(0) exit() Sum = [0] * (N + 1) for i in range(N): Sum[i + 1] = Sum[i] + a[i] for i in range(1,N - 1): mid = a[i] k = N - 1 - i delta = min(i,k) end = delta start = 0 if a[0] - mid < mid - a[i + 1]: continue while end - start > 1: mm = end + start >> 1 if a[mm] - mid < mid - a[i + mm + 1]: end = mm else: start = mm ue = Sum[start + 1] sita = Sum[i + start + 1 + 1] - Sum[i + 1] tmp = ue - mid * (start + 1) * 2 + sita if tmp > ans: ans = tmp print(ans)