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, pj|
    if pi.last > pj.last
      [pj.last, pi.last]
    elsif  pi.last < pj.last
      [pi.last, pj.last]
    elsif pi.first > pj.first
      [pi.first, pi.last]
    else
      [pj.first, pi.last]
    end
  }
  s += p0.reduce(0) { |s, pi| s + pi.first }
  n -= 1
end
p s


