import Data.List (partition)

main :: IO ()
main = do
  getLine
  hs <- map (read :: String -> Int) . words <$> getLine
  print (f hs)

f :: [Int] -> Int
f [] = 0
f xs
  =
    let
        m = minimum xs
        xss = splits (fmap (subtract m) xs)
      in
        m + sum (fmap f xss)

splits :: [Int] -> [[Int]]
splits [] = []
splits xs
  =
    let
        (px, sx) = partition (> 0) xs
        sx' = dropWhile (== 0) sx
      in
        if null px then splits sx' else px : splits sx'
