import Control.Applicative

readInts :: IO [Int]
readInts = map read . words <$> getLine

f :: [Int] -> Int -> Int
f [] b = 0
f (a:ax) b = if a >= b then f ax a else f ax b + b - a


main :: IO ()
main = do
    n <- readLn :: IO Int
    a <- readInts
    print $ f a 0