n = int(input())
a = list(map(int,input().split()))
b = [(a[i],i+1) for i in range(n)]
b.sort(reverse=True)
cntf = 0
cntb = n-1
dp = [[0 for i in range(n+1)] for j in range(n+1)]
dp[0][1] = -10**18
for i in range(1,n+1):
  x,ind = b[i-1]
  dp[i][0] = dp[i-1][0]+abs(n-i-ind+1)*x
  for j in range(1,i+1):
    dp[i][j] = max(dp[i-1][j-1]+abs(j-ind)*x,dp[i-1][j]+abs(n-(i-j)+1-ind)*x)
print(max(dp[n]))