#![allow(unused_imports)]
#![allow(unused_variables)]
#![allow(dead_code)]
use proconio::input;
use proconio::marker::*;
use std::cmp::{min, max};
use std::collections::{HashSet, HashMap, BinaryHeap};

struct UnionFind{
    n: usize,
    parent: Vec<usize>,
    size: Vec<usize>,
}

impl UnionFind{
    fn new(n: usize ) -> UnionFind{
        let mut parent = vec![0; n];
        for i in 0..n {
            parent[i] = i;
        }
        let size = vec![1; n];
        UnionFind{n, parent, size}
    }

    fn root(&mut self, x: usize) -> usize{
        if x == self.parent[x]{
            self.parent[x]
        }else{
            let res = self.root(self.parent[x]);
            self.parent[x] = res;
            self.parent[x]
        }
    }

    fn unite(&mut self, x: usize, y: usize){
        let a = self.root(x);
        let b = self.root(y);
        if a == b{
            return;
        }
        if self.size[a] <= self.size[b]{
            self.parent[b] = a;
            self.size[a] += self.size[b];
        }else{
            self.parent[a] = b;
            self.size[b] += self.size[a];
        }
    }

    fn is_same(&mut self, x: usize, y: usize) -> bool{
        self.root(x) == self.root(y)
    }

    fn get_size(&mut self, x: usize) -> usize{
        let a = self.root(x);
        self.size[a]
    }
}

fn main() {
    input! {
        n: usize,
        a: [i32; n],
    }
    let mut ans = 0;
    let mut t = a[0];
    for i in 1..n{
        ans += max(t - a[i], 0);
        t = max(t, a[i]);
    }
    println!("{}", ans);
}
