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


(n-2).times do
  target_index=0
  target = Float::INFINITY
  (a.length-2).times do |index|
    if a[index+1] < target
      target = a[index+1]
      target_index = index+1
    end
  end

  left = a[1]
  right = a[-2]
  tmp = (left < right) ? left : right
  tmp_index = (left < right) ? 1 : -2

  if tmp < target*2
    target = tmp
    target_index = tmp_index
  end

  a[target_index-1]+=target
  a[target_index+1]+=target
  a[target_index]=nil
  a=a.compact
#p a
end

#p a
p a.inject(:+)
