import           Control.Monad
import qualified Data.ByteString.Char8 as B
import           Data.Char
import qualified Data.Vector.Unboxed   as U

readIntN :: Int -> IO (U.Vector Int)
readIntN n = U.unfoldrN n (B.readInt . B.dropWhile isSpace) <$> B.getLine

main :: IO ()
main = do
  n <- readLn
  as <- readIntN n
  print $ solve as

solve :: U.Vector Int -> Int
solve = fst . U.foldl' f (0, 0)
 where
  f (acc, h) x
    | h <= x    = (acc, h `max` x)
    | otherwise = (acc + h - x, h)
