n = gets.to_i
p0 = gets.split.map { |s| [s.to_i] }
s = 0
while n > 1
  pa = p0[0..n - 2]
  pb = p0[1..n - 1]
  p0 = pa.zip(pb).map { |pi| pi.flatten.uniq.sort.last(2) }
  s += p0.reduce(0) { |s, pi| s + pi.first }
  n -= 1
end
p s
