use proconio::{fastout, input};

#[fastout]
fn main() {
    input! {
        n: u32,
        mut a: [u64; n],
    }

    let mut height: u128 = 0;
    loop {
        if a.len() < 2 { break; }
        let i = max_index(&a);

        if i == a.len()-1 { break; }
        let max = a[i];
        for j in i+1..a.len() {
            height += max as u128 - a[j] as u128;
        }
        
        if i == 0 { break; }

        a.resize(i, 0);
    }

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

fn max_index(array: &[u64]) -> usize {
    let mut i = 0;

    for (j, &elem) in array.iter().enumerate() {
        if elem > array[i] {
            i = j;
        }
    }

    i
}
