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::<i64>();
  let mut high:i64 = 0;
  let mut ans: i64 = 0;
  for i in a {
    if high > i {
      ans += high - i;
    } else {
      high = i;
    }
  }
  println!("{}", ans);
}