N = int(input())
As = [(i+1, a) for i, a in enumerate(map(int, input().split()))]
As.sort(key=lambda x: x[1], reverse=True)
 
dp = [[0 for _ in range(N+1)] for _ in range(N+1)] # dp[x*N+y] = max when l=x r=y

r = 0
for n in range(1, N+1): # x+y = n
    i, a = As[n-1]
    for m in range(n+1): # x = m
        x = m
        y = n - m
        if x > 0:
            tl = dp[x-1][y] + a * abs(i - x)
        else:
            tl = 0
        if y > 0:
            tr = dp[x][y-1] + a * abs((N-y+1) - i)
        else:
            tr = 0
        dp[x][y] = max(tl, tr)
        r = max(r, dp[x][y])
 
print(r)