import java.util.Scanner;

public class Main {
	public static void main(String [] args) {
    	Scanner sn = new Scanner(System.in);
      	int n = sn.nextInt();
      	int x[] = new int[n];
      	for(int i = 0; i < n; i++) {
        	x[i] = sn.nextInt();
        }
      	Long res = 0L;
      	int max = x[0];
      	for(int i = 1; i < n; i++) {
        	if( max > x[i]) {
            	res = res + max - x[i]; 
            } else {
            	max = x[i];
            }
        }
      	System.out.println(res);
    }
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String [] args) {
    	Scanner sn = new Scanner(System.in);
      	int n = sn.nextInt();
      	int x[] = new int[n];
      	for(int i = 0; i < n; i++) {
        	x[i] = sn.nextInt();
        }
      	Long res = 0L;
      	int max = x[0];
      	for(int i = 1; i < n; i++) {
        	if( max > x[i]) {
            	res = res + max - x[i]; 
            } else {
            	max = x[i];
            }
        }
      	System.out.println(res);
    }
} Main public static void main(String [] args) {
    	Scanner sn = new Scanner(System.in);
      	int n = sn.nextInt();
      	int x[] = new int[n];
      	for(int i = 0; i < n; i++) {
        	x[i] = sn.nextInt();
        }
      	Long res = 0L;
      	int max = x[0];
      	for(int i = 1; i < n; i++) {
        	if( max > x[i]) {
            	res = res + max - x[i]; 
            } else {
            	max = x[i];
            }
        }
      	System.out.println(res);
    } main {
    	Scanner sn = new Scanner(System.in);
      	int n = sn.nextInt();
      	int x[] = new int[n];
      	for(int i = 0; i < n; i++) {
        	x[i] = sn.nextInt();
        }
      	Long res = 0L;
      	int max = x[0];
      	for(int i = 1; i < n; i++) {
        	if( max > x[i]) {
            	res = res + max - x[i]; 
            } else {
            	max = x[i];
            }
        }
      	System.out.println(res);
    } Scanner sn = new Scanner(System.in); sn new Scanner(System.in) int n = sn.nextInt(); n sn.nextInt() sn.nextInt sn int x[] = new int[n]; x new int[n] n for(int i = 0; i < n; i++) {
        	x[i] = sn.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
        	x[i] = sn.nextInt();
        } {
        	x[i] = sn.nextInt();
        } x[i] = sn.nextInt() x[i] x i sn.nextInt() sn.nextInt sn Long res = 0L; res 0L int max = x[0]; max x[0] x 0 for(int i = 1; i < n; i++) {
        	if( max > x[i]) {
            	res = res + max - x[i]; 
            } else {
            	max = x[i];
            }
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
        	if( max > x[i]) {
            	res = res + max - x[i]; 
            } else {
            	max = x[i];
            }
        } {
        	if( max > x[i]) {
            	res = res + max - x[i]; 
            } else {
            	max = x[i];
            }
        } if( max > x[i]) {
            	res = res + max - x[i]; 
            } else {
            	max = x[i];
            } max > x[i] max x[i] x i {
            	res = res + max - x[i]; 
            } res = res + max - x[i] res res + max - x[i] res + max res max x[i] x i {
            	max = x[i];
            } max = x[i] max x[i] x i System.out.println(res) System.out.println System.out System System.out res String [] args args public class Main {
	public static void main(String [] args) {
    	Scanner sn = new Scanner(System.in);
      	int n = sn.nextInt();
      	int x[] = new int[n];
      	for(int i = 0; i < n; i++) {
        	x[i] = sn.nextInt();
        }
      	Long res = 0L;
      	int max = x[0];
      	for(int i = 1; i < n; i++) {
        	if( max > x[i]) {
            	res = res + max - x[i]; 
            } else {
            	max = x[i];
            }
        }
      	System.out.println(res);
    }
} public class Main {
	public static void main(String [] args) {
    	Scanner sn = new Scanner(System.in);
      	int n = sn.nextInt();
      	int x[] = new int[n];
      	for(int i = 0; i < n; i++) {
        	x[i] = sn.nextInt();
        }
      	Long res = 0L;
      	int max = x[0];
      	for(int i = 1; i < n; i++) {
        	if( max > x[i]) {
            	res = res + max - x[i]; 
            } else {
            	max = x[i];
            }
        }
      	System.out.println(res);
    }
} Main
import java.util.Scanner;

public class Main {
	public static void main(String [] args) {
    	Scanner sn = new Scanner(System.in);
      	int n = sn.nextInt();
      	int x[] = new int[n];
      	for(int i = 0; i < n; i++) {
        	x[i] = sn.nextInt();
        }
      	Long res = 0L;
      	int max = x[0];
      	for(int i = 1; i < n; i++) {
        	if( max > x[i]) {
            	res = res + max - x[i]; 
            } else {
            	max = x[i];
            }
        }
      	System.out.println(res);
    }
}