main = do
    getLine
    hs <- fmap read. words <$> getLine
    print $ solve hs 0

solve hs count
    | all (0==) hs = count
    | otherwise = solve (uncurry g $ span (==0) hs) (count+1)
    where
        g a b = a ++ zipWith (-) b (f b)
        f [] = []
        f (x:xs)
            | x>0 = 1 : f xs
            | otherwise = 0 : fmap (const 0) xs