N = gets.to_i
h = gets.split.map(&:to_i)
h.unshift(0)
h.push(0)
count = 0
now = false
count_b = 0


while true do
  for i in 1..N do
    if !now && h[i] > 0 && h[i-1] == 0
      h[i] -= 1
      if h[i+1] == 0
        count += 1
      else
        now = true
      end
    elsif now && h[i] > 0
      h[i] -= 1
      if h[i+1] == 0
        now = false
        count += 1
      end
    end
  end

  break if count == count_b
  count_b = count
end

puts count
