#![allow(unused_imports)]
#![allow(unused_variables)]

use itertools::Itertools;
use proconio::marker::*;
use proconio::*;
use std::cmp;

#[fastout]
fn main() {
    input! {
        n: usize,
        a: [i32; n],
    }
    // annotate for rust-analyzer
    let n: usize = n;
    let mut a: Vec<i32> = a;

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

    println!("{}", ans);
}
