Scanf.scanf "%d" (fun n ->
    let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun a -> a)) in

    let rec loop i prev acc =
        if i = n then acc else
            if a.(i) >= prev then loop (i + 1) a.(i) acc else
                let d = prev - a.(i) in
                loop (i + 1) prev (acc + d)
    in
    loop 0 0 0 |> Printf.printf "%d\n"
)