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

al_id = [(a, idx) for idx, a in enumerate(al)]
al_id = sorted(al_id, reverse=True)
dp = [[-float("inf") for i in range(n+1)] for j in range(n+1)]
dp[0][0] = 0
for count, (a, idx) in enumerate(al_id):
    for i in range(count+1):
        j = count - i
        dp[i+1][j] = max(dp[i][j] + abs(idx-i) * a, dp[i+1][j])
        dp[i][j+1] = max(dp[i][j] + abs(n-j-idx-1) * a, dp[i][j+1])


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

print(ans)