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

ary = []
a.each_with_index{|x, i| ary.push([x, i]) }

ary = ary.sort{|lhs, rhs| rhs[0] <=> lhs[0] }

left = 0
right = 0

res = 0
ary.each do |el|
  res += [el[1] - left, (n - right - 1) - el[1]].max * el[0]
end

puts res