import sys
from collections import defaultdict

sys.setrecursionlimit(4000)


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

  dp = defaultdict(int)

  def cal(ln, rn):
    if ln + rn > n:
      return

    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)

    cal(ln + 1, rn)
    cal(ln, rn + 1)

  cal(1, 0)
  cal(0, 1)

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


main()
