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| [elem, index]}
C = B.each do |b|
  b << [b[0] * (b[1]-left), b[0] * (right-b[1])].max
end
C = B.sort_by { |b| b[2] }.reverse
C.each do |c|
  if c[1] - left >= right - c[1]
    result += c[0] * (c[1]-left)
    left += 1
  else
    result += c[0] * (right - c[1])
    right -= 1
  end
end

p result