N = gets.to_i
A = gets.chomp.split.map(&:to_i)

A = A.map.with_index { |elem, index| [index, elem]}
A = A.sort_by { |a| a[1] }.reverse
dpn = { "0,0" => 0 }
dpn1 = {}

(1..N).each do |n|
  dpn.each do |d|

    x = d[0].split(",")[0].to_i
    y = d[0].split(",")[1].to_i
    n_left = x
    n_right = N-1-y

    # left
    if A[n-1][0] >= n_left
      l = d[1] + A[n-1][1] * (A[n-1][0] - n_left)
      if dpn1["#{x+1},#{y}"].nil?
        dpn1["#{x+1},#{y}"] = l
      else
        dpn1["#{x+1},#{y}"] = l if l > dpn1["#{x+1},#{y}"]
      end
    end
    
    # right
    if A[n-1][0] <= n_right
      r = d[1] + A[n-1][1] * (A[n-1][0] - n_right).abs
      if  dpn1["#{x},#{y+1}"].nil?
        dpn1["#{x},#{y+1}"] = r 
      else
        dpn1["#{x},#{y+1}"] = r if r > dpn1["#{x},#{y+1}"]
      end
    end
  end
  dpn = dpn1
  # p dpn
  dpn1 = {}
end
p dpn.max { |x, y| x[1] <=> y[1] }[1]