import qualified Data.ByteString.Char8 as BS
import Data.Char
import Data.List

main = do
  li <- BS.getLine
  let Just (n,_) = BS.readInt li
  li <- BS.getLine
  let as = unfoldr (BS.readInt . BS.dropWhile isSpace) li
  let ans = compute n as
  print ans

compute :: Int -> [Int] -> Int
compute n (a:as) = loop 0 a as

loop c _ [] = c
loop c p (a:as)
  | p <= a = loop c a as
  | True = let c1 = c + p - a in seq c1 $ loop c1 p as