import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner (System.in);
		
		long n =sc.nextInt();
		
		int height1=sc.nextInt();
		int height2=0;
		
		int takadai=0;
		long sum=0;
		
		for(int i=0;i<(n-1);i++) {
			height2 = sc.nextInt();
			if(height1>height2) {
				takadai = height1 - height2;
				sum = sum + takadai;
			}else {
				height1 = height2; 
			}
		}
		
		System.out.println(sum);
	}

} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner (System.in);
		
		long n =sc.nextInt();
		
		int height1=sc.nextInt();
		int height2=0;
		
		int takadai=0;
		long sum=0;
		
		for(int i=0;i<(n-1);i++) {
			height2 = sc.nextInt();
			if(height1>height2) {
				takadai = height1 - height2;
				sum = sum + takadai;
			}else {
				height1 = height2; 
			}
		}
		
		System.out.println(sum);
	}

} Main public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner (System.in);
		
		long n =sc.nextInt();
		
		int height1=sc.nextInt();
		int height2=0;
		
		int takadai=0;
		long sum=0;
		
		for(int i=0;i<(n-1);i++) {
			height2 = sc.nextInt();
			if(height1>height2) {
				takadai = height1 - height2;
				sum = sum + takadai;
			}else {
				height1 = height2; 
			}
		}
		
		System.out.println(sum);
	} main {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner (System.in);
		
		long n =sc.nextInt();
		
		int height1=sc.nextInt();
		int height2=0;
		
		int takadai=0;
		long sum=0;
		
		for(int i=0;i<(n-1);i++) {
			height2 = sc.nextInt();
			if(height1>height2) {
				takadai = height1 - height2;
				sum = sum + takadai;
			}else {
				height1 = height2; 
			}
		}
		
		System.out.println(sum);
	} Scanner sc = new Scanner (System.in); sc new Scanner (System.in) long n =sc.nextInt(); n sc.nextInt() sc.nextInt sc int height1=sc.nextInt(); height1 sc.nextInt() sc.nextInt sc int height2=0; height2 0 int takadai=0; takadai 0 long sum=0; sum 0 for(int i=0;i<(n-1);i++) {
			height2 = sc.nextInt();
			if(height1>height2) {
				takadai = height1 - height2;
				sum = sum + takadai;
			}else {
				height1 = height2; 
			}
		} int i=0; int i=0; i 0 i<(n-1) i (n-1) n 1 i++ i++ i {
			height2 = sc.nextInt();
			if(height1>height2) {
				takadai = height1 - height2;
				sum = sum + takadai;
			}else {
				height1 = height2; 
			}
		} {
			height2 = sc.nextInt();
			if(height1>height2) {
				takadai = height1 - height2;
				sum = sum + takadai;
			}else {
				height1 = height2; 
			}
		} height2 = sc.nextInt() height2 sc.nextInt() sc.nextInt sc if(height1>height2) {
				takadai = height1 - height2;
				sum = sum + takadai;
			}else {
				height1 = height2; 
			} height1>height2 height1 height2 {
				takadai = height1 - height2;
				sum = sum + takadai;
			} takadai = height1 - height2 takadai height1 - height2 height1 height2 sum = sum + takadai sum sum + takadai sum takadai {
				height1 = height2; 
			} height1 = height2 height1 height2 System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner (System.in);
		
		long n =sc.nextInt();
		
		int height1=sc.nextInt();
		int height2=0;
		
		int takadai=0;
		long sum=0;
		
		for(int i=0;i<(n-1);i++) {
			height2 = sc.nextInt();
			if(height1>height2) {
				takadai = height1 - height2;
				sum = sum + takadai;
			}else {
				height1 = height2; 
			}
		}
		
		System.out.println(sum);
	}

} public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner (System.in);
		
		long n =sc.nextInt();
		
		int height1=sc.nextInt();
		int height2=0;
		
		int takadai=0;
		long sum=0;
		
		for(int i=0;i<(n-1);i++) {
			height2 = sc.nextInt();
			if(height1>height2) {
				takadai = height1 - height2;
				sum = sum + takadai;
			}else {
				height1 = height2; 
			}
		}
		
		System.out.println(sum);
	}

} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner (System.in);
		
		long n =sc.nextInt();
		
		int height1=sc.nextInt();
		int height2=0;
		
		int takadai=0;
		long sum=0;
		
		for(int i=0;i<(n-1);i++) {
			height2 = sc.nextInt();
			if(height1>height2) {
				takadai = height1 - height2;
				sum = sum + takadai;
			}else {
				height1 = height2; 
			}
		}
		
		System.out.println(sum);
	}

}