N = gets,chomp.to_i
Ps = gets.chomp.split(" ").map(&:to_i)
sum = 0

Ps.each_with_index do |e,i|
  Qs = Ps.dup.shift(i+1)
  max = e
  secmax = 0
  Qs.each do |f|
    if f >= max
      secmax = max
      max = f
    elsif f >= secmax
      secmax = f
    end
    sum += secmax
  end
end

puts sum