import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        Main m = new Main(sc);
        m.show(m.solve());
        sc.close();
    }
    
	//フィールド
    Scanner sc;
    int N;
    int[] A;
    
    //入力
    Main(Scanner sc) {
        this.sc = sc;
        this.N = sc.nextInt();
        A = new int[N];
        for (int i=0; i<N; i++) {
        	A[i] = sc.nextInt();
        }
    }
    
    //解答処理
    private long solve() {
        long answer = 0;
        
        for (int i=1; i<N; i++) {
        	if (A[i] < A[i-1]) {
        		long tmp = A[i-1]-A[i];
        		answer += tmp;
        		A[i] += tmp;	//踏み台の分を身長に加算
        	}
        }
        
        return answer;
    }
    
    //出力
    public void show(long answer) {
        System.out.println(answer);
    }

} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        Main m = new Main(sc);
        m.show(m.solve());
        sc.close();
    }
    
	//フィールド
    Scanner sc;
    int N;
    int[] A;
    
    //入力
    Main(Scanner sc) {
        this.sc = sc;
        this.N = sc.nextInt();
        A = new int[N];
        for (int i=0; i<N; i++) {
        	A[i] = sc.nextInt();
        }
    }
    
    //解答処理
    private long solve() {
        long answer = 0;
        
        for (int i=1; i<N; i++) {
        	if (A[i] < A[i-1]) {
        		long tmp = A[i-1]-A[i];
        		answer += tmp;
        		A[i] += tmp;	//踏み台の分を身長に加算
        	}
        }
        
        return answer;
    }
    
    //出力
    public void show(long answer) {
        System.out.println(answer);
    }

} Main //フィールド
    Scanner sc; sc int N; N int[] A; A public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        Main m = new Main(sc);
        m.show(m.solve());
        sc.close();
    } main {
        Scanner sc = new Scanner(System.in);
        Main m = new Main(sc);
        m.show(m.solve());
        sc.close();
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) Main m = new Main(sc); m new Main(sc) m.show(m.solve()) m.show m m.solve() m.solve m sc.close() sc.close sc String[] args args //入力
    Main(Scanner sc) {
        this.sc = sc;
        this.N = sc.nextInt();
        A = new int[N];
        for (int i=0; i<N; i++) {
        	A[i] = sc.nextInt();
        }
    } Main {
        this.sc = sc;
        this.N = sc.nextInt();
        A = new int[N];
        for (int i=0; i<N; i++) {
        	A[i] = sc.nextInt();
        }
    } this.sc = sc this.sc this this.sc sc this.N = sc.nextInt() this.N this this.N sc.nextInt() sc.nextInt sc A = new int[N] A new int[N] N for (int i=0; i<N; i++) {
        	A[i] = sc.nextInt();
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
        	A[i] = sc.nextInt();
        } {
        	A[i] = sc.nextInt();
        } A[i] = sc.nextInt() A[i] A i sc.nextInt() sc.nextInt sc Scanner sc sc //解答処理
    private long solve() {
        long answer = 0;
        
        for (int i=1; i<N; i++) {
        	if (A[i] < A[i-1]) {
        		long tmp = A[i-1]-A[i];
        		answer += tmp;
        		A[i] += tmp;	//踏み台の分を身長に加算
        	}
        }
        
        return answer;
    } solve {
        long answer = 0;
        
        for (int i=1; i<N; i++) {
        	if (A[i] < A[i-1]) {
        		long tmp = A[i-1]-A[i];
        		answer += tmp;
        		A[i] += tmp;	//踏み台の分を身長に加算
        	}
        }
        
        return answer;
    } long answer = 0; answer 0 for (int i=1; i<N; i++) {
        	if (A[i] < A[i-1]) {
        		long tmp = A[i-1]-A[i];
        		answer += tmp;
        		A[i] += tmp;	//踏み台の分を身長に加算
        	}
        } int i=1; int i=1; i 1 i<N i N i++ i++ i {
        	if (A[i] < A[i-1]) {
        		long tmp = A[i-1]-A[i];
        		answer += tmp;
        		A[i] += tmp;	//踏み台の分を身長に加算
        	}
        } {
        	if (A[i] < A[i-1]) {
        		long tmp = A[i-1]-A[i];
        		answer += tmp;
        		A[i] += tmp;	//踏み台の分を身長に加算
        	}
        } if (A[i] < A[i-1]) {
        		long tmp = A[i-1]-A[i];
        		answer += tmp;
        		A[i] += tmp;	//踏み台の分を身長に加算
        	} A[i] < A[i-1] A[i] A i A[i-1] A i-1 i 1 {
        		long tmp = A[i-1]-A[i];
        		answer += tmp;
        		A[i] += tmp;	//踏み台の分を身長に加算
        	} long tmp = A[i-1]-A[i]; tmp A[i-1]-A[i] A[i-1] A i-1 i 1 A[i] A i answer += tmp answer tmp A[i] += tmp A[i] A i tmp return answer; answer //出力
    public void show(long answer) {
        System.out.println(answer);
    } show {
        System.out.println(answer);
    } System.out.println(answer) System.out.println System.out System System.out answer long answer answer public class Main {
	public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        Main m = new Main(sc);
        m.show(m.solve());
        sc.close();
    }
    
	//フィールド
    Scanner sc;
    int N;
    int[] A;
    
    //入力
    Main(Scanner sc) {
        this.sc = sc;
        this.N = sc.nextInt();
        A = new int[N];
        for (int i=0; i<N; i++) {
        	A[i] = sc.nextInt();
        }
    }
    
    //解答処理
    private long solve() {
        long answer = 0;
        
        for (int i=1; i<N; i++) {
        	if (A[i] < A[i-1]) {
        		long tmp = A[i-1]-A[i];
        		answer += tmp;
        		A[i] += tmp;	//踏み台の分を身長に加算
        	}
        }
        
        return answer;
    }
    
    //出力
    public void show(long answer) {
        System.out.println(answer);
    }

} public class Main {
	public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        Main m = new Main(sc);
        m.show(m.solve());
        sc.close();
    }
    
	//フィールド
    Scanner sc;
    int N;
    int[] A;
    
    //入力
    Main(Scanner sc) {
        this.sc = sc;
        this.N = sc.nextInt();
        A = new int[N];
        for (int i=0; i<N; i++) {
        	A[i] = sc.nextInt();
        }
    }
    
    //解答処理
    private long solve() {
        long answer = 0;
        
        for (int i=1; i<N; i++) {
        	if (A[i] < A[i-1]) {
        		long tmp = A[i-1]-A[i];
        		answer += tmp;
        		A[i] += tmp;	//踏み台の分を身長に加算
        	}
        }
        
        return answer;
    }
    
    //出力
    public void show(long answer) {
        System.out.println(answer);
    }

} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        Main m = new Main(sc);
        m.show(m.solve());
        sc.close();
    }
    
	//フィールド
    Scanner sc;
    int N;
    int[] A;
    
    //入力
    Main(Scanner sc) {
        this.sc = sc;
        this.N = sc.nextInt();
        A = new int[N];
        for (int i=0; i<N; i++) {
        	A[i] = sc.nextInt();
        }
    }
    
    //解答処理
    private long solve() {
        long answer = 0;
        
        for (int i=1; i<N; i++) {
        	if (A[i] < A[i-1]) {
        		long tmp = A[i-1]-A[i];
        		answer += tmp;
        		A[i] += tmp;	//踏み台の分を身長に加算
        	}
        }
        
        return answer;
    }
    
    //出力
    public void show(long answer) {
        System.out.println(answer);
    }

}
