
import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		try(Scanner sc = new Scanner(System.in)) {
			
			int n = sc.nextInt();
			
			long sum = 0;
			long prev = 0;
			
			for(int i = 0 ; i < n ; i++ ) {
				long current = sc.nextLong();
				
				if ( current < prev ) {
					sum += prev - current;
				}
				else {
					prev = current;
				}
			}
			
			System.out.println(sum);
		}
	}

} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args){
		try(Scanner sc = new Scanner(System.in)) {
			
			int n = sc.nextInt();
			
			long sum = 0;
			long prev = 0;
			
			for(int i = 0 ; i < n ; i++ ) {
				long current = sc.nextLong();
				
				if ( current < prev ) {
					sum += prev - current;
				}
				else {
					prev = current;
				}
			}
			
			System.out.println(sum);
		}
	}

} Main public static void main(String[] args){
		try(Scanner sc = new Scanner(System.in)) {
			
			int n = sc.nextInt();
			
			long sum = 0;
			long prev = 0;
			
			for(int i = 0 ; i < n ; i++ ) {
				long current = sc.nextLong();
				
				if ( current < prev ) {
					sum += prev - current;
				}
				else {
					prev = current;
				}
			}
			
			System.out.println(sum);
		}
	} main {
		try(Scanner sc = new Scanner(System.in)) {
			
			int n = sc.nextInt();
			
			long sum = 0;
			long prev = 0;
			
			for(int i = 0 ; i < n ; i++ ) {
				long current = sc.nextLong();
				
				if ( current < prev ) {
					sum += prev - current;
				}
				else {
					prev = current;
				}
			}
			
			System.out.println(sum);
		}
	} try(Scanner sc = new Scanner(System.in)) {
			
			int n = sc.nextInt();
			
			long sum = 0;
			long prev = 0;
			
			for(int i = 0 ; i < n ; i++ ) {
				long current = sc.nextLong();
				
				if ( current < prev ) {
					sum += prev - current;
				}
				else {
					prev = current;
				}
			}
			
			System.out.println(sum);
		} {
			
			int n = sc.nextInt();
			
			long sum = 0;
			long prev = 0;
			
			for(int i = 0 ; i < n ; i++ ) {
				long current = sc.nextLong();
				
				if ( current < prev ) {
					sum += prev - current;
				}
				else {
					prev = current;
				}
			}
			
			System.out.println(sum);
		} Scanner sc = new Scanner(System.in) Scanner sc = new Scanner(System.in) new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc long sum = 0; sum 0 long prev = 0; prev 0 for(int i = 0 ; i < n ; i++ ) {
				long current = sc.nextLong();
				
				if ( current < prev ) {
					sum += prev - current;
				}
				else {
					prev = current;
				}
			} int i = 0 ; int i = 0 ; i 0 i < n i n i++ i++ i {
				long current = sc.nextLong();
				
				if ( current < prev ) {
					sum += prev - current;
				}
				else {
					prev = current;
				}
			} {
				long current = sc.nextLong();
				
				if ( current < prev ) {
					sum += prev - current;
				}
				else {
					prev = current;
				}
			} long current = sc.nextLong(); current sc.nextLong() sc.nextLong sc if ( current < prev ) {
					sum += prev - current;
				}
				else {
					prev = current;
				} current < prev current prev {
					sum += prev - current;
				} sum += prev - current sum prev - current prev current {
					prev = current;
				} prev = current prev current 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){
		try(Scanner sc = new Scanner(System.in)) {
			
			int n = sc.nextInt();
			
			long sum = 0;
			long prev = 0;
			
			for(int i = 0 ; i < n ; i++ ) {
				long current = sc.nextLong();
				
				if ( current < prev ) {
					sum += prev - current;
				}
				else {
					prev = current;
				}
			}
			
			System.out.println(sum);
		}
	}

} public class Main {
	public static void main(String[] args){
		try(Scanner sc = new Scanner(System.in)) {
			
			int n = sc.nextInt();
			
			long sum = 0;
			long prev = 0;
			
			for(int i = 0 ; i < n ; i++ ) {
				long current = sc.nextLong();
				
				if ( current < prev ) {
					sum += prev - current;
				}
				else {
					prev = current;
				}
			}
			
			System.out.println(sum);
		}
	}

} Main

import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		try(Scanner sc = new Scanner(System.in)) {
			
			int n = sc.nextInt();
			
			long sum = 0;
			long prev = 0;
			
			for(int i = 0 ; i < n ; i++ ) {
				long current = sc.nextLong();
				
				if ( current < prev ) {
					sum += prev - current;
				}
				else {
					prev = current;
				}
			}
			
			System.out.println(sum);
		}
	}

}