splitNotZero :: [Int] -> [[Int]]
splitNotZero [] = []
splitNotZero xs
  | null others = [notZeros]
  | otherwise   = ([notZeros] ++) $ splitNotZero $ tail others
  where (notZeros, others) = break (== 0) xs

solve :: [Int] -> Int
solve []  = 0
solve xs
  | min == 0  = sum $ map solve $ splitNotZero xs
  | otherwise = (+ min) $ solve $ map (subtract min) xs
  where min = minimum xs

main :: IO ()
main = do
  _ <- getLine
  heights <- map read . words <$> getLine
  print $ solve heights