import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {


    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine().trim());
        String[] s=br.readLine().split(" ");
        long[] a=new long[n];
        long res=0;
        for(int i=0;i<n;i++){
            a[i]=Long.parseLong(s[i].trim());
        }

        long last=a[0];
        for(int i=1;i<n;i++){
            if(a[i]<last){
                res+=(last-a[i]);
            }
            last=Math.max(last,a[i]);
        }
        System.out.println(res);





    }






} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {


    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine().trim());
        String[] s=br.readLine().split(" ");
        long[] a=new long[n];
        long res=0;
        for(int i=0;i<n;i++){
            a[i]=Long.parseLong(s[i].trim());
        }

        long last=a[0];
        for(int i=1;i<n;i++){
            if(a[i]<last){
                res+=(last-a[i]);
            }
            last=Math.max(last,a[i]);
        }
        System.out.println(res);





    }






} Main public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine().trim());
        String[] s=br.readLine().split(" ");
        long[] a=new long[n];
        long res=0;
        for(int i=0;i<n;i++){
            a[i]=Long.parseLong(s[i].trim());
        }

        long last=a[0];
        for(int i=1;i<n;i++){
            if(a[i]<last){
                res+=(last-a[i]);
            }
            last=Math.max(last,a[i]);
        }
        System.out.println(res);





    } main {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine().trim());
        String[] s=br.readLine().split(" ");
        long[] a=new long[n];
        long res=0;
        for(int i=0;i<n;i++){
            a[i]=Long.parseLong(s[i].trim());
        }

        long last=a[0];
        for(int i=1;i<n;i++){
            if(a[i]<last){
                res+=(last-a[i]);
            }
            last=Math.max(last,a[i]);
        }
        System.out.println(res);





    } BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) int n=Integer.parseInt(br.readLine().trim()); n Integer.parseInt(br.readLine().trim()) Integer.parseInt Integer br.readLine().trim() br.readLine().trim br.readLine() br.readLine br String[] s=br.readLine().split(" "); s br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " long[] a=new long[n]; a new long[n] n long res=0; res 0 for(int i=0;i<n;i++){
            a[i]=Long.parseLong(s[i].trim());
        } int i=0; int i=0; i 0 i<n i n i++ i++ i {
            a[i]=Long.parseLong(s[i].trim());
        } {
            a[i]=Long.parseLong(s[i].trim());
        } a[i]=Long.parseLong(s[i].trim()) a[i] a i Long.parseLong(s[i].trim()) Long.parseLong Long s[i].trim() s[i].trim s[i] s i long last=a[0]; last a[0] a 0 for(int i=1;i<n;i++){
            if(a[i]<last){
                res+=(last-a[i]);
            }
            last=Math.max(last,a[i]);
        } int i=1; int i=1; i 1 i<n i n i++ i++ i {
            if(a[i]<last){
                res+=(last-a[i]);
            }
            last=Math.max(last,a[i]);
        } {
            if(a[i]<last){
                res+=(last-a[i]);
            }
            last=Math.max(last,a[i]);
        } if(a[i]<last){
                res+=(last-a[i]);
            } a[i]<last a[i] a i last {
                res+=(last-a[i]);
            } res+=(last-a[i]) res (last-a[i]) last a[i] a i last=Math.max(last,a[i]) last Math.max(last,a[i]) Math.max Math last a[i] a 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) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine().trim());
        String[] s=br.readLine().split(" ");
        long[] a=new long[n];
        long res=0;
        for(int i=0;i<n;i++){
            a[i]=Long.parseLong(s[i].trim());
        }

        long last=a[0];
        for(int i=1;i<n;i++){
            if(a[i]<last){
                res+=(last-a[i]);
            }
            last=Math.max(last,a[i]);
        }
        System.out.println(res);





    }






} public class Main {


    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine().trim());
        String[] s=br.readLine().split(" ");
        long[] a=new long[n];
        long res=0;
        for(int i=0;i<n;i++){
            a[i]=Long.parseLong(s[i].trim());
        }

        long last=a[0];
        for(int i=1;i<n;i++){
            if(a[i]<last){
                res+=(last-a[i]);
            }
            last=Math.max(last,a[i]);
        }
        System.out.println(res);





    }






} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {


    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine().trim());
        String[] s=br.readLine().split(" ");
        long[] a=new long[n];
        long res=0;
        for(int i=0;i<n;i++){
            a[i]=Long.parseLong(s[i].trim());
        }

        long last=a[0];
        for(int i=1;i<n;i++){
            if(a[i]<last){
                res+=(last-a[i]);
            }
            last=Math.max(last,a[i]);
        }
        System.out.println(res);





    }






}
