import sys
input = sys.stdin.readline

n = int(input())
A = list(map(int,input().split()))

B = [[A[i], i] for i in range(n)]
B.sort(reverse=True)

DP = [[0] * (n+1) for i in range(n)]

DP[0][0] = B[0][0] * B[0][1]
DP[0][1] = B[0][0] * (n-1-B[0][1])


for i in range(1, n):
    ind = B[i][1]
    for j in range(i+2):
        if j-1 >= 0:
            DP[i][j] = DP[i-1][j-1] + B[i][0] * abs(n-1-(j-1)-ind)
        # print(i,j,ind, i+1-j, ind-(i+1-j))
        DP[i][j] = max(DP[i][j], DP[i-1][j] + B[i][0] * abs(ind-(i-1+1-j)))
# print(DP)
print(max(DP[-1]))