def second_sum(n, p)
  sum = 0
  0.upto(n - 2) do |l|
    tmp = [p[l]]
    (l + 1).upto(n - 1) do |r|
      tmp << p[r]
      tmp.sort!
      tmp.shift if tmp.size > 2
      sum += tmp[0]
    end
    puts
  end
  sum
end

n = gets.to_i
p = gets.split.map(&:to_i)

puts second_sum(n, p)