n = gets.to_i
b = gets.split(' ').map(&:to_i)

a = []
a << b[0]
a << b[0]

b.each_cons(2) do |i, j|
  if i <= j
    a << j
  else
    a.delete_at(a.length-1)
    a << j
    a << j
  end
end

print "#{a.inject(:+)}"