calc :: [Int] -> Int -> Int
calc x highest
  |x == [] = 0
  |otherwise = calc(tail x) higher + (higher - front)
  where front = head x
        higher = max front highest

main = do
    _ <- getLine
    x <- map read . words <$> getLine
    putStrLn(show(calc x 0))