use proconio::{input};

fn main() {
    input! {
        n: usize,
        alist: [u32; n],
    }

    let mut ans: u32 = 0;
    let mut prev: u32 = 0;
    for a in alist {
        if prev > a {
            let step = prev - a;
            ans += step;
            prev = a + step;
        } else {
            prev = a;
        }
    }

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