n = gets.to_i
a = gets.split(' ').map &:to_i

map = {}
a.each_with_index do |a, i|
 map[i+1] = a
end

sum = 0
l = 1
r = n
while !map.empty?
 pos = map.key(map.values.max)
 r_sum = (r - pos).abs
 l_sum = (pos - l).abs
 # puts [l, r, pos, a[pos-1], r_sum, l_sum, sum].inspect

 if r_sum >= l_sum
  sum += r_sum * a[pos-1]
  r -= 1
 else
  sum += l_sum * a[pos-1]
  l += 1
 end

 map.delete pos
end

puts sum