
n = parse(Int, readline())
arr = [tpl for tpl in enumerate(parse.(Int, split(readline())))]
arr = sort(arr, by = tpl -> -tpl[2])

dp = Dict()

for ln = 0 : n
  for rn = 0 : n - ln
    if ln + rn > n || get(dp, (ln, rn), 0) > 0
      continue
    end

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

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

println(maximum([dp[i, n - i] for i in 0 : n]))
