let readNInt () =
  let temp = read_line () in
  let digits = String.split_on_char ' ' temp in
  List.map int_of_string digits


let rec findDifSum acc l =
  match l with
    [] -> acc
  | a :: [] -> acc
  | a :: b :: rest ->
     if a > b
     then (findDifSum (a - b + acc) rest)
     else (findDifSum acc rest)
  
let main () =
  let _ = read_int () in
  let l = readNInt () in
  let sum = findDifSum 0 (List.rev l) in
  (
    print_int sum;
    print_newline ()
  )

let _ = main ()
