import Control.Monad -- for replicateM_
import Data.List -- for sort


{-
deal :: IO()
deal = do 
    [n, x] <- map read . words <$> getLine
    info <- replicateM (n-1) getWeight
    -- graph <- buildTree info [[[]]]
    putStrLn $ show info

main :: IO()
main = getLine >>= flip replicateM_ deal . read 
-}

solve :: [Integer] -> Integer -> Integer
solve (x:xs) nowMax
    | null xs = delta
    | otherwise = delta + solve xs newMax
    where 
       delta = if nowMax > x then nowMax - x else 0 
       newMax = if x > nowMax then x else nowMax

main :: IO()
main = do 
    getLine
    li <- map read . words <$> getLine
    putStrLn $ show $ solve li 0
