import sys
input = sys.stdin.readline

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

cnt = [0,n-1]

As = sorted(list(zip(A,range(n))), reverse = True)

dp = [[0]*(n+1) for _ in range(n+1)]

# 配る
for i in range(n):
    for j in range(i+1):
        a,p = As[i]
        dp[i+1][j] = max(dp[i+1][j],dp[i][j] + a * (n-1 - p -i+j))
        dp[i+1][j+1] = max(dp[i+1][j+1],dp[i][j] + a * (p - j))

print(max(dp[n]))
