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

INF = float("inf")
MX = 2005
dp = [[-INF for _ in range(MX)] for _ in range(MX)]
dp[0][0] = 0
p = []
for i in range(N):
  p.append([A[i], i])
p.sort(reverse = True)
for i in range(N):
  pi = p[i][1]
  for l in range(i + 1):
    r = i - l
    dp[i + 1][l + 1] = max(dp[i + 1][l + 1], dp[i][l] + (pi - l) * A[pi])
    dp[i + 1][l] = max(dp[i + 1][l], dp[i][l] + ((N - r - 1) - pi) * A[pi])

ans = 0
for i in range(N + 1):
  ans = max(ans, dp[N][i])
print(ans)