N = int(input())
A = [(int(v), i) for i, v in enumerate(input().split())]
A = sorted(A, key=lambda x: x[1])
dp = [[0 for j in range(2005)] for i in range(2005)]
for i in range(N):
    pi = A[i][1]
    for l in range(i+1):
        r = i - l
        dp[i+1][l+1] = max(dp[i+1][l+1], dp[i][l] + (pi - l) * A[pi][0])
        dp[i+1][l] = max(dp[i+1][l], dp[i][l] + ((N-r-1) - pi) * A[pi][0])

ans = 0
for i in range(N+1):
    ans = max(ans, dp[N][i])

print(ans)