use proconio::input;

fn main() {
    input! {
        n:usize,
        a: [i32;n],
    }

    let mut ans = 0;
    let mut prev_height = 0;
    for i in 0..n {
        if a[i] - prev_height < 0 {
            ans += prev_height - a[i];
        } else {
            prev_height = a[i];
        }
    }
    println!("{}", ans)
}
