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

def mizuyari(flowers, n)
  l = 0
  ls = false
  flowers.each do |flower|
    if flower == 0 && !ls
      l += 1
    else
      ls = true
      break
    end
  end

  r = l
  flowers[l..n-1].each do |flower|
    if flower == 0
      break
    else
      r += 1
    end
  end

  r -= 1

  [l, r]
end

count = 0

until flowerInput.all?{|f| f==0}
  res = mizuyari(flowerInput, n)
  # puts flowerInput[res[0]..res[1]]
  eachcount = 0
  flowerInput.each do |flo|
    flowerInput[eachcount] = flo - 1 if eachcount.between?(res[0], res[1])
    eachcount += 1
  end
  count += 1
end

puts count