let () =
  Scanf.scanf "%d\n" @@ fun n ->
  let lst = Array.to_list
      (Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun d -> d) in
  let (ans,_) = List.fold_left
      (fun (a,bfr) x -> if x < bfr then (a+(bfr-x),bfr) else (a,x))
      (0, List.hd lst) (List.tl lst) in
  Printf.printf "%d\n" ans