import Control.Monad
import qualified Data.ByteString.Char8 as B  
import qualified Data.Vector.Unboxed as VU
import Data.Char

solve :: VU.Vector Int -> Int -> Int
solve xs acc
    | VU.null xs || VU.all (==0) xs = acc 
    | otherwise = solve (VU.map (subtract 1) ts) (acc+1) + (solve ds' 0)
    where
        ds = VU.dropWhile (<=0) xs 
        ts = VU.takeWhile (>0) ds
        ds' = VU.drop (VU.length ts) ds

main = do
    n <- readLn :: IO Int
    hs <- VU.unfoldrN n (B.readInt . B.dropWhile isSpace) <$> B.getLine
    print $ solve hs 0