use std::rc::Rc;

pub fn main() {
  fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse::<T>().ok().unwrap()
  }
  fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace().map(|e| e.parse().ok().unwrap()).collect()
  }

  let _ = read::<i16>();
  let a = read_vec::<i32>();
  let ans = a.into_iter().fold((0 as i32, 0 as i64), |a, b|
    if a.0 > b {(a.0, a.1+ (a.0  - b) as i64)}
    else {(b, a.1)}).1;
  println!("{}", ans);

}
