let () = Scanf.scanf "%d\n" @@ fun n ->
  let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun a -> a in
  Printf.printf "%d\n" @@
  fst @@
  Array.fold_left (fun (sum, prev) a ->
    sum + max prev a - a, max prev a) (0, min_int) as_

