import           Data.Maybe
import qualified Data.ByteString.Char8         as C8

getInts :: IO [Int]
getInts = fmap (map (fst . fromJust . C8.readInt) . C8.words) C8.getLine

main :: IO ()
main = do
    _  <- getLine
    hs <- getInts
    print $ solve hs - 1

solve :: [Int] -> Int
solve [] = 0
solve hs = (1 +) . sum . map (solve . map (subtract 1)) . splitOn 0 $ hs

splitOn :: Eq a => a -> [a] -> [[a]]
splitOn _ [] = [[]]
splitOn a (x : xs) | x == a    = [] : res
                   | otherwise = (x : h) : hs
    where res@(h : hs) = splitOn a xs
