fn c() {
    input! {
        n: usize,
        a: [u32; n]
    }

    let mut ans = 0;
    let mut check = a[0];

    for v in a.iter() {
        if *v > check {
            check = *v;
        }
        else if *v < check {
            ans += (check - *v);
        }
    }

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

use proconio::{input, marker::Chars};

fn main() {
    c();
}