class Youji
  attr_accessor :active, :index, :max_u, :to_index, :u
  def initialize(active, index, max_index)
    @active = active
    @index = index
    @max_u = [max_index - index, index - 1].max * active
  end

  def u(i = nil)
    if i
      (index - i).abs * active
    else
      @u
    end
  end

  def move(i)
    @to_index = i
    @u = u(i)
  end

  def to_s
    "[active=#{@active}, index=#{@index}->#{@to_index}, u=#{@u}, um=#{@max_u}]"
  end
end

N = $stdin.gets.to_i
A = []
$stdin.gets.split.map {|i| i.to_i }.each_with_index do |a, i|
  A.push(Youji.new(a, i, N))
end
A.sort! {|x, y| d = y.max_u <=> x.max_u; (d != 0) ? d : ( y.active <=> x.active ) }
#$stderr.puts A
#$stderr.puts "-"
sortedA = Array.new(N)
s = 0
e = N - 1
preX = nil
A.each do |x|
  su = x.u(s)
  eu = x.u(e)
  puts "-#{x}/#{su}/#{eu}"
  if su > eu
    sortedA[s] = x
    x.move(s)
    s += 1
    if preX
      sortedA[e] = preX
      preX.move(e)
      e -= 1
      preX = nil
    end
  elsif su < eu
    sortedA[e] = x
    x.move(e)
    e -= 1
    if preX
      sortedA[s] = preX
      preX.move(s)
      s += 1
      preX = nil
    end
  else
    if preX
      sortedA[s] = preX
      preX.move(s)
      s += 1
      sortedA[e] = x
      x.move(e)
      e -= 1
    else
      preX = x
    end
  end
end
if preX
  sortedA[s] = preX
  preX.move(s)
end

#$stderr.puts sortedA.map {|a| a.to_s }

sum = 0
sortedA.each do |x|
  sum += x.u
end

puts sum
