let n = stdin.ReadLine() |> int
let a = stdin.ReadLine().Split() |> Array.map int64 |> Array.toList

let rec step list max sum =
    match list with
    | head :: tail ->
        if head > max then step tail head sum
        else step tail max (sum+max-head)
    | [] -> sum

let head,tail = List.head a, List.tail a

(step tail head 0L) |> stdout.WriteLine