N = gets.to_i
A = gets.chomp.split.map(&:to_i)
left = 0
right = N-1
result = 0

B = A.map.with_index { |elem, index| [index, elem]}
C = B.sort_by { |b| b[1] }.reverse
dpn = { [0,0] => 0 }
dpn1 = {}
(1..N).each do |n|
  (0..n).each do |x|
    y = n - x
    list = []
    if x == 0
      dpn1[[0,y]] = dpn[[0,y-1]] + C[n-1][1] * (C[n-1][0] - (right - y + 1)).abs
    elsif y == 0
      dpn1[[x,0]] = dpn[[x-1,0]] + C[n-1][1] * (C[n-1][0] - (left + x - 1)).abs
    else
      dpn1[[x,y]] = [dpn[[x,y-1]] + C[n-1][1] * (C[n-1][0] - (right - y + 1)).abs,  dpn[[x-1,y]] + C[n-1][1] * (C[n-1][0] - (left + x - 1)).abs].max
    end
  end
  dpn = dpn1
  dpn1 = {}
end
p dpn.max { |x, y| x[1] <=> y[1] }[1]