// -*- coding:utf-8-unix -*-

use proconio::input;

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

    let a: Vec<i32> = a;
    let mut max = 0;
    let sum = a
        .iter()
        .map(|&ai| {
            if max < ai {
                max = ai;
            }
            max - ai
        })
        .sum::<i32>();

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