module Main where

import Control.Monad
import Control.Applicative
import Data.List

showingList :: [Int] -> String
showingList (x:[]) = show x
showingList (x:xs) = (show x) ++ " " ++ (showingList xs)

solve :: ([Int], Int) -> Int
solve ([], t) = 0
solve ((x:xs), t) = if x >= t then solve (xs, x) else solve (xs, t) + t - x

main :: IO ()
main = do
    n <- getLine
    a <- map read . words <$> getLine :: IO [Int]
    putStrLn $ show ( solve (a, 0))
