import sys

sys.setrecursionlimit(4000)

n = int(input())
arr = map(int, input().split())
arr = tuple(sorted(enumerate(arr), key=lambda tpl: -tpl[1]))

dp = {}
for ln in range(n + 1):
  for rn in range(n + 1 - ln):
    if ln + rn > n or dp.get((ln, rn)):
      continue

    if ln == rn == 0:
      dp[0, 0] = 0
    else:
      p, a = arr[ln + rn - 1]
      val_l = dp[ln - 1, rn] + a * abs(p - (ln - 1)) if ln > 0 else 0
      val_r = dp[ln, rn - 1] + a * abs(p - (n - rn)) if rn > 0 else 0

      dp[ln, rn] = max(val_l, val_r)

print(max([dp[i, n - i] for i in range(n + 1)]))
