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

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

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