import java.util.Scanner;
import java.util.Arrays;


public class Main{
    public static void main(String[] args){
        Scanner sc =new Scanner(System.in);
        int n = sc.nextInt();
/*        int[] a = new int[n]
        for(int i=0;i<n;i++){
            a[i]=sc.nextInt();
        }
*/      
        int x = 0;
        int y = 0;
        long diff = 0;
        x= sc.nextInt();
        for(int i=0; i<n-1; i++){
            y= sc.nextInt();
//            System.out.print(y+" ");
            if(y<x){
                diff = diff+x-y;
            }else if(x<y){
                x=y;
            }
//            System.out.println(x);
        }
            System.out.println(diff);
    }
} import java.util.Scanner; Scanner java.util import java.util.Arrays; Arrays java.util public class Main{
    public static void main(String[] args){
        Scanner sc =new Scanner(System.in);
        int n = sc.nextInt();
/*        int[] a = new int[n]
        for(int i=0;i<n;i++){
            a[i]=sc.nextInt();
        }
*/      
        int x = 0;
        int y = 0;
        long diff = 0;
        x= sc.nextInt();
        for(int i=0; i<n-1; i++){
            y= sc.nextInt();
//            System.out.print(y+" ");
            if(y<x){
                diff = diff+x-y;
            }else if(x<y){
                x=y;
            }
//            System.out.println(x);
        }
            System.out.println(diff);
    }
} Main public static void main(String[] args){
        Scanner sc =new Scanner(System.in);
        int n = sc.nextInt();
/*        int[] a = new int[n]
        for(int i=0;i<n;i++){
            a[i]=sc.nextInt();
        }
*/      
        int x = 0;
        int y = 0;
        long diff = 0;
        x= sc.nextInt();
        for(int i=0; i<n-1; i++){
            y= sc.nextInt();
//            System.out.print(y+" ");
            if(y<x){
                diff = diff+x-y;
            }else if(x<y){
                x=y;
            }
//            System.out.println(x);
        }
            System.out.println(diff);
    } main {
        Scanner sc =new Scanner(System.in);
        int n = sc.nextInt();
/*        int[] a = new int[n]
        for(int i=0;i<n;i++){
            a[i]=sc.nextInt();
        }
*/      
        int x = 0;
        int y = 0;
        long diff = 0;
        x= sc.nextInt();
        for(int i=0; i<n-1; i++){
            y= sc.nextInt();
//            System.out.print(y+" ");
            if(y<x){
                diff = diff+x-y;
            }else if(x<y){
                x=y;
            }
//            System.out.println(x);
        }
            System.out.println(diff);
    } Scanner sc =new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int x = 0; x 0 int y = 0; y 0 long diff = 0; diff 0 x= sc.nextInt() x sc.nextInt() sc.nextInt sc for(int i=0; i<n-1; i++){
            y= sc.nextInt();
//            System.out.print(y+" ");
            if(y<x){
                diff = diff+x-y;
            }else if(x<y){
                x=y;
            }
//            System.out.println(x);
        } int i=0; int i=0; i 0 i<n-1 i n-1 n 1 i++ i++ i {
            y= sc.nextInt();
//            System.out.print(y+" ");
            if(y<x){
                diff = diff+x-y;
            }else if(x<y){
                x=y;
            }
//            System.out.println(x);
        } {
            y= sc.nextInt();
//            System.out.print(y+" ");
            if(y<x){
                diff = diff+x-y;
            }else if(x<y){
                x=y;
            }
//            System.out.println(x);
        } y= sc.nextInt() y sc.nextInt() sc.nextInt sc if(y<x){
                diff = diff+x-y;
            }else if(x<y){
                x=y;
            } y<x y x {
                diff = diff+x-y;
            } diff = diff+x-y diff diff+x-y diff+x diff x y if(x<y){
                x=y;
            } x<y x y {
                x=y;
            } x=y x y System.out.println(diff) System.out.println System.out System System.out diff String[] args args public class Main{
    public static void main(String[] args){
        Scanner sc =new Scanner(System.in);
        int n = sc.nextInt();
/*        int[] a = new int[n]
        for(int i=0;i<n;i++){
            a[i]=sc.nextInt();
        }
*/      
        int x = 0;
        int y = 0;
        long diff = 0;
        x= sc.nextInt();
        for(int i=0; i<n-1; i++){
            y= sc.nextInt();
//            System.out.print(y+" ");
            if(y<x){
                diff = diff+x-y;
            }else if(x<y){
                x=y;
            }
//            System.out.println(x);
        }
            System.out.println(diff);
    }
} public class Main{
    public static void main(String[] args){
        Scanner sc =new Scanner(System.in);
        int n = sc.nextInt();
/*        int[] a = new int[n]
        for(int i=0;i<n;i++){
            a[i]=sc.nextInt();
        }
*/      
        int x = 0;
        int y = 0;
        long diff = 0;
        x= sc.nextInt();
        for(int i=0; i<n-1; i++){
            y= sc.nextInt();
//            System.out.print(y+" ");
            if(y<x){
                diff = diff+x-y;
            }else if(x<y){
                x=y;
            }
//            System.out.println(x);
        }
            System.out.println(diff);
    }
} Main
import java.util.Scanner;
import java.util.Arrays;


public class Main{
    public static void main(String[] args){
        Scanner sc =new Scanner(System.in);
        int n = sc.nextInt();
/*        int[] a = new int[n]
        for(int i=0;i<n;i++){
            a[i]=sc.nextInt();
        }
*/      
        int x = 0;
        int y = 0;
        long diff = 0;
        x= sc.nextInt();
        for(int i=0; i<n-1; i++){
            y= sc.nextInt();
//            System.out.print(y+" ");
            if(y<x){
                diff = diff+x-y;
            }else if(x<y){
                x=y;
            }
//            System.out.println(x);
        }
            System.out.println(diff);
    }
}