use proconio::fastout;
use proconio::input;

#[fastout]
fn main() {
    input! {
        n: usize,
        an: [i32; n],
    }
    let mut prev = 0;
    let mut ans = 0;
    for &a in &an {
        if prev < a {
            prev = a;
        } else {
            ans += prev - a;
        }
    }
    println!("{}", ans);
}
