use proconio::input;

fn main() {
    input! {
        n: usize,
        a: [i32; n],
    }
    let mut res = 0i32;
    let mut mx = a[0];
    for i in 1..n {
        if a[i]>mx {
            mx = a[i];
        } else {
            res += mx-a[i];
        }
    }
    println!("{}", res);
}
