use proconio::input;

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

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

    for tall in talls {
        println!("before: {}, tall: {}", before, tall);

        if before > tall {
            sum += before - tall;
        }

        if before < tall {
            before = tall;
        }
    }

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