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

use proconio::input;

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

    let sum = (a as Vec<i32>)
        .iter()
        .fold((0, 0), |(sum, max), &ai| {
            if max < ai {
                (sum, ai)
            } else {
                (sum + max - ai, max)
            }
        })
        .0;

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