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

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

    let mut height = a[0];
    let mut sum = 0;

    for person in a {
        if height > person {
            sum += height - person;
        } else {
            height = person;
        }
    }
    println!("{}", sum);
}