N = int(input())
active = list(map(int, input().split(" ")))
active = [ [active[i],i+1] for i in range(N) ]
active.sort(reverse=True)
dp = [ [0]*(N+1) for _ in range(N+1) ]

for l in range(1, N+1) :
    idx = l-1
    a = active[idx]
    for i in range(l) :
        dp[l-i][i] = max(dp[l-i][i], dp[l-i-1][i] + abs(l-i-a[1])*a[0] ) 
        dp[i][l-i] = max(dp[i][l-i], dp[i][l-i-1] + abs(N+1-l+i-a[1])*a[0] ) 

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

print(ans)