import Control.Monad
import Control.Applicative
import Data.List
import qualified Data.Vector as V
import Debug.Trace as De

func :: Int -> Int -> Int
func a b
  | a < b = b
  | otherwise = a

main = do
  n <- readLn :: IO Int
  a1 <- getLine
  let a = map (read :: String -> Int) (words a1)
  let b = scanl func 0 a
  let ans = (sum b) - (sum a)
  putStrLn $ show ans