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] * ((N+1)**2) # dp[x*N+y] = max when l=x r=y

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)*N+y] + a * abs(i - x)
        else:
            tl = 0
        if y > 0:
            tr = dp[x*N+(y-1)] + a * abs((N-y+1) - i)
        else:
            tr = 0
        dp[m*N+(n-m)] = max(tl, tr)

r = 0
for n in range(N+1):
    x = n
    y = N - n
    r = max(dp[x*N+y], r)

print(r)
