N = int(input()) A = list(map(int,input().split())) A.sort() ans = 0 tmp = A[0] n = 1 flag = 0 for i in range(1,N//2+1): tmp += A[i] + A[N - i] n += 2 tmp2 = tmp - n * A[i] if tmp2 >= ans: ans = tmp2 else: break print(ans)