import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))

B = np.sort(A)[::-1]
B_index = np.argsort(A)[::-1]
dp = [[0]*(N+1) for _ in range(N+1)]

for x in range(0,N+1):
    for y in range(0,N+1-x):
        ind = B_index[x+y-1]
        if x==0 and y==0:
            dp[x][y] = 0
        elif x==0:
            dp[x][y] = dp[x][y-1] + B[x+y-1]*abs(ind-((N-1)-(y-1)))
        elif y==0:
            dp[x][y] = dp[x-1][y]+B[x+y-1]*abs(ind-(x-1))
        else:
            dp[x][y] = max(dp[x-1][y]+B[x+y-1]*abs(ind-(x-1)),dp[x][y-1] + B[x+y-1]*abs(ind-((N-1)-(y-1))))

print(np.max(dp))