import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as BS
import Data.List

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

main = do
    getLine
    hs <- getIntList
    print $ solve hs

solve [] = 0
solve hs = m + solve (map (flip (-) m) hsa) + solve hsb
    where hs' = dropWhile (== 0) hs
          (hsa, hsb) = span (/=0) hs'
          m | null hsa = 0
            | otherwise = minimum hsa