
module Main where

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


solve :: [Int] -> Int
solve list = snd $ s list (0, 0)
  where
    s (p:ps) (prev, total) = s ps (if p > prev then (p, total + p - prev) else (p, total))
    s [] r = r
