import java.util.*;
public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		// 整数の入力(スペース改行関係なし)
		int n = sc.nextInt();
        long soukei = 0;
      	int amae = 0;

      	for(int i = 0 ; i < n ; i++){
        	int a = sc.nextInt();
        	if(a < amae){
            	soukei = soukei + (amae-a);
            }else{
            	amae = a;
            }
        	
        }

		System.out.println(soukei);

		
	}
} import java.util.*; util.* java public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		// 整数の入力(スペース改行関係なし)
		int n = sc.nextInt();
        long soukei = 0;
      	int amae = 0;

      	for(int i = 0 ; i < n ; i++){
        	int a = sc.nextInt();
        	if(a < amae){
            	soukei = soukei + (amae-a);
            }else{
            	amae = a;
            }
        	
        }

		System.out.println(soukei);

		
	}
} Main public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		// 整数の入力(スペース改行関係なし)
		int n = sc.nextInt();
        long soukei = 0;
      	int amae = 0;

      	for(int i = 0 ; i < n ; i++){
        	int a = sc.nextInt();
        	if(a < amae){
            	soukei = soukei + (amae-a);
            }else{
            	amae = a;
            }
        	
        }

		System.out.println(soukei);

		
	} main {
		Scanner sc = new Scanner(System.in);
		// 整数の入力(スペース改行関係なし)
		int n = sc.nextInt();
        long soukei = 0;
      	int amae = 0;

      	for(int i = 0 ; i < n ; i++){
        	int a = sc.nextInt();
        	if(a < amae){
            	soukei = soukei + (amae-a);
            }else{
            	amae = a;
            }
        	
        }

		System.out.println(soukei);

		
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc long soukei = 0; soukei 0 int amae = 0; amae 0 for(int i = 0 ; i < n ; i++){
        	int a = sc.nextInt();
        	if(a < amae){
            	soukei = soukei + (amae-a);
            }else{
            	amae = a;
            }
        	
        } int i = 0 ; int i = 0 ; i 0 i < n i n i++ i++ i {
        	int a = sc.nextInt();
        	if(a < amae){
            	soukei = soukei + (amae-a);
            }else{
            	amae = a;
            }
        	
        } {
        	int a = sc.nextInt();
        	if(a < amae){
            	soukei = soukei + (amae-a);
            }else{
            	amae = a;
            }
        	
        } int a = sc.nextInt(); a sc.nextInt() sc.nextInt sc if(a < amae){
            	soukei = soukei + (amae-a);
            }else{
            	amae = a;
            } a < amae a amae {
            	soukei = soukei + (amae-a);
            } soukei = soukei + (amae-a) soukei soukei + (amae-a) soukei (amae-a) amae a {
            	amae = a;
            } amae = a amae a System.out.println(soukei) System.out.println System.out System System.out soukei String[] args args public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		// 整数の入力(スペース改行関係なし)
		int n = sc.nextInt();
        long soukei = 0;
      	int amae = 0;

      	for(int i = 0 ; i < n ; i++){
        	int a = sc.nextInt();
        	if(a < amae){
            	soukei = soukei + (amae-a);
            }else{
            	amae = a;
            }
        	
        }

		System.out.println(soukei);

		
	}
} public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		// 整数の入力(スペース改行関係なし)
		int n = sc.nextInt();
        long soukei = 0;
      	int amae = 0;

      	for(int i = 0 ; i < n ; i++){
        	int a = sc.nextInt();
        	if(a < amae){
            	soukei = soukei + (amae-a);
            }else{
            	amae = a;
            }
        	
        }

		System.out.println(soukei);

		
	}
} Main
import java.util.*;
public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		// 整数の入力(スペース改行関係なし)
		int n = sc.nextInt();
        long soukei = 0;
      	int amae = 0;

      	for(int i = 0 ; i < n ; i++){
        	int a = sc.nextInt();
        	if(a < amae){
            	soukei = soukei + (amae-a);
            }else{
            	amae = a;
            }
        	
        }

		System.out.println(soukei);

		
	}
}