import sys
from collections import defaultdict

sys.setrecursionlimit(4000)

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

dp = defaultdict(int)

def cal(ln, rn):
  if ln < 0 or rn < 0 or ln == rn == 0:
    return 0

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

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

  return dp[ln, rn]


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