n = int(input())
a = list(map(int, input().split()))
b = [(i, a[i]) for i in range(n)]
b = sorted(b, key=lambda x: x[1])[::-1]

dp = [[0] * (n+1) for _ in range(n+1)]
ans = 0
for l in range(0, n+1):
    for r in range(0, n+1):
        if l + r == n:
            ans = max(ans, dp[l][r])
            break
        idx, activeness = b[l+r]
        dp[l+1][r] = max(dp[l+1][r], 
                         dp[l][r] + abs(idx - l) * activeness)
        dp[l][r+1] = max(dp[l][r+1],
                         dp[l][r] + (n-1-r-idx) * activeness)

print(ans)
        