n = gets.to_i
happy = gets.split.map(&:to_i)
happy.unshift 0 # ダミー

# 貪欲法
# happyが多い順にインデックスを並び替える
sorted_index = (1..n).sort_by{|i| -happy[i]}

total = 0
first = 1
last = n
sorted_index.each do |i|
  first_dist = (i - first).abs
  last_dist = (last - i).abs
  if first_dist > last_dist
    total += happy[i] * first_dist
    first += 1
  else
    total += happy[i] * last_dist
    last -= 1
  end
end

puts total
