solve [] b = []
solve (a:as) b
    | a < b = (b-a) : solve (as) b
    | otherwise = 0 : solve (as) a

main = do
    n <- readLn :: IO Int
    an <- map read . words <$> getLine :: IO [Int]
    print $ sum $ solve an 0
