#![allow(unused_imports, dead_code, unused_variables, unused_mut)]
use std::cmp;
use std::mem::swap;
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
use permutohedron::LexicalPermutation;
use text_io::{read, scan};
use proconio::input;
const INF: i32 = std::i32::MAX;
const MOD: i32 = 1e9 as i32 + 7;

fn main() {
    input! {n: i32, a: [i32; n]};

    let mut a: Vec<i32> = a;

    let mut flg = false;

    let mut ans = 0;
    for i in 1..n as usize {
        if a[i - 1] > a[i] {
            ans += a[i - 1] - a[i];
            a[i] = a[i - 1];
        }
    }

    println!("{}", ans);

}