n = gets.to_i
result = Array.new(n)
as = gets.split(" ").map(&:to_i)

min_index = 0
max_index = n - 1

n.times do
  potentials = as.map.with_index do |v, i|
    if i - min_index < max_index - i
      [v * (max_index - i), i, max_index]
    else
      [v * (i - min_index), i, min_index]
    end
  end

  max = potentials.max{|a, b| a[0] <=> b[0]}
  result[max[2]] = max[0]
  as[max[1]] = 0
  
  min_index += 1 if min_index == max[2]
  max_index -= 1 if max_index == max[2]
end

p result
puts result.sum

