N=int(input())
A=list(map(int,input().split()))
B=[]
for k in range(N):
    B.append([A[k],k+1])
B.sort(key=lambda x: x[0], reverse=True)
dp=[[0 for k in range(N+1)]for k in range(N+1)]

for k in range(N):
    for j in range(k+1):
        if k>j:
            dp[j+1][k-j]=max(dp[j][k-j]+B[k][0]*(B[k][1]-j-1), dp[j+1][k-j-1]+B[k][0]*(N-k+j+1-B[k][1]))
        if k==j:
            dp[j+1][0]=dp[j][0]+B[k][0]*(B[k][1]-j-1)
        if j >0:
            dp[j][k-j+1]=max(dp[j-1][k-j+1]+B[k][0]*(B[k][1]-j), dp[j][k-j]+B[k][0]*(N-k+j-B[k][1]))
        if j==0:
            dp[0][k-j+1]=dp[0][k-j]+B[k][0]*(N-k+j-B[k][1])
ans=[]
for k in range(N+1):
    ans.append(dp[k][N-k])
print(max(ans))               