fn main() {
    let s = {
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).unwrap();
        s.trim_right().to_owned()
    };

    let t = {
        let mut t = String::new();
        std::io::stdin().read_line(&mut t).unwrap();
        t.trim_right().to_owned()
    };

    let n = {
        let mut ws = s.split_whitespace();
        let n: i32 = ws.next().unwrap().parse().unwrap();
        n
    };

    let mut a = {
        let mut ws = t.split_whitespace();

        let mut a : Vec<i32> = Vec::new();
        for i in ws {
            a.push(i.parse().unwrap());
        }
        a
    };

    let mut sum : i32 = 0;

    for i in 0..(n - 1) as usize  {
        if a[i + 1] - a[i] < 0 {
            sum += a[i] - a[i + 1];
            a[i + 1] = a[i];
        }
    }

    println!("{:?}", a);

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