let N =
    stdin.ReadLine()
    |> int
let A =
    stdin.ReadLine().Split()
    |> Array.map int

A
|> Array.fold (fun (sum,highest) item ->
    if item < highest then (sum+int64(highest-item),highest)
    else (sum, item)) (0L,0)
|> fun (sum,_) -> sum
|> stdout.WriteLine