import qualified Data.Text as T
import Debug.Trace

main = do
    n <- readLn :: IO Int
    hs <- map read . words <$> getLine :: IO [Int]
    print $ solve 0 hs

solve :: Int -> [Int] -> Int
solve n hs = case hasZero of
    -- True -> trace ("spl : " ++ show splitted)$ foldl solve n splitted 
    True -> foldl solve n splitted 
        where 
            splitted = filter (/=[]) $ map (filter (/=0)) $ split hs [[0]]
    False -> solve n (waterALL hs) + 1 
    where 
        hasZero = 0 `elem` hs


waterALL :: [Int] -> [Int]
waterALL [] = []
waterALL (x:xs) = (x-1) : waterALL xs

split :: [Int] -> [[Int]] -> [[Int]]
split [] res = res
-- split (x:xs) ([]) = if x /= 0 then split xs [[x]] else split xs []
split (x:xs) (y:ys) = if x /= 0 then split xs ((x:y):ys) else split xs ([0]:y:ys) 
