require 'pp'

n = gets.chomp!.to_i
p_n = gets.chomp!.split(" ").map(&:to_i)

memo = Array.new(n, 0)

sum = 0
0.upto(n-2) do |i|
    max1 = 0
    max2 = 0
    i.upto(n-1) do |j|
        if max1 < p_n[j]
            max2 = max1
            max1 = p_n[j]
        elsif max2 < p_n[j]
            max2 = p_n[j]
        end

        #puts "#{i}, #{j}, #{max1}, #{max2}"
        sum += max2
    end
end

puts sum
