use proconio::{fastout, input};

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

    let mut height = 0;
    loop {
        let i = max_index(&a);

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

        a.split_off(i);
    }

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

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

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

    i
}
