import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
sa = [(a[i], i) for i in range(N)]
sa.sort(key = lambda x: (-x[0], -abs(x[1] - (N - 1) / 2)))
dp = [[0] * (N + 1) for _ in range(N + 1)]
for k in range(N):
  x, i = sa[k]
  for l in range(k + 1):
    r = k - l
    nr = N - 1 - r
    dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + abs(l - i) * x)
    dp[l][r + 1] = max(dp[l][r + 1], dp[l][r] + abs(nr - i) * x)
res = 0
for l in range(N):
  r = N - l
  res = max(res, dp[l][r])
print(res)