use std::io;
use std::cmp::max;
//use std::vec;
// fn sum_of_string(s: &String) -> u32 {
//     s.chars().fold(0, |acc, c| c.to_digit(10).unwrap_or(0) + acc)
//   }

fn main(){

    let mut n:String = String::new();

    io::stdin().read_line(&mut n).expect("Required Input");

    let _n: i128 = n.trim().parse().unwrap();

    let mut stol_height: i128 = 0;
    let mut input: String = String::new();
    let mut prev_height: i128 = 0;

    io::stdin().read_line(&mut input).expect("Input required");

    let values: Vec<i128> = input
            .split_whitespace()
            .map(|x| x.parse::<i128>())
            .collect::<Result<Vec<i128>, _>>()
            .unwrap();

    for i in values.iter(){

        if prev_height <= *i{
            prev_height = max(prev_height, *i);
            continue;
        }

        stol_height += prev_height-*i;

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

}