use proconio::input;

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

    let mut before = talls[0];
    let mut sum = 0;

    for mut tall in talls {
        if before > tall {
            sum += before - tall;
            tall += before - tall;
        }

        before = tall;
    }

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