import java.util.*;

public class Main{
        public static void main(String[] args){
                Scanner sc = new Scanner(System.in);
                String str = sc.nextLine();
        int N = Integer.parseInt(str);

                long count=0;
        int prev_person = sc.nextInt();

        for(int i = 1; i < N; i++){
            int now_person = sc.nextInt();
            if(now_person < prev_person){
                count += prev_person - now_person;
            }
            else{
                prev_person = now_person;
            }
        }


                System.out.println(count);
                sc.close();
        }
}
 import java.util.*; util.* java public class Main{
        public static void main(String[] args){
                Scanner sc = new Scanner(System.in);
                String str = sc.nextLine();
        int N = Integer.parseInt(str);

                long count=0;
        int prev_person = sc.nextInt();

        for(int i = 1; i < N; i++){
            int now_person = sc.nextInt();
            if(now_person < prev_person){
                count += prev_person - now_person;
            }
            else{
                prev_person = now_person;
            }
        }


                System.out.println(count);
                sc.close();
        }
} Main public static void main(String[] args){
                Scanner sc = new Scanner(System.in);
                String str = sc.nextLine();
        int N = Integer.parseInt(str);

                long count=0;
        int prev_person = sc.nextInt();

        for(int i = 1; i < N; i++){
            int now_person = sc.nextInt();
            if(now_person < prev_person){
                count += prev_person - now_person;
            }
            else{
                prev_person = now_person;
            }
        }


                System.out.println(count);
                sc.close();
        } main {
                Scanner sc = new Scanner(System.in);
                String str = sc.nextLine();
        int N = Integer.parseInt(str);

                long count=0;
        int prev_person = sc.nextInt();

        for(int i = 1; i < N; i++){
            int now_person = sc.nextInt();
            if(now_person < prev_person){
                count += prev_person - now_person;
            }
            else{
                prev_person = now_person;
            }
        }


                System.out.println(count);
                sc.close();
        } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) String str = sc.nextLine(); str sc.nextLine() sc.nextLine sc int N = Integer.parseInt(str); N Integer.parseInt(str) Integer.parseInt Integer str long count=0; count 0 int prev_person = sc.nextInt(); prev_person sc.nextInt() sc.nextInt sc for(int i = 1; i < N; i++){
            int now_person = sc.nextInt();
            if(now_person < prev_person){
                count += prev_person - now_person;
            }
            else{
                prev_person = now_person;
            }
        } int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
            int now_person = sc.nextInt();
            if(now_person < prev_person){
                count += prev_person - now_person;
            }
            else{
                prev_person = now_person;
            }
        } {
            int now_person = sc.nextInt();
            if(now_person < prev_person){
                count += prev_person - now_person;
            }
            else{
                prev_person = now_person;
            }
        } int now_person = sc.nextInt(); now_person sc.nextInt() sc.nextInt sc if(now_person < prev_person){
                count += prev_person - now_person;
            }
            else{
                prev_person = now_person;
            } now_person < prev_person now_person prev_person {
                count += prev_person - now_person;
            } count += prev_person - now_person count prev_person - now_person prev_person now_person {
                prev_person = now_person;
            } prev_person = now_person prev_person now_person System.out.println(count) System.out.println System.out System System.out count sc.close() sc.close sc String[] args args public class Main{
        public static void main(String[] args){
                Scanner sc = new Scanner(System.in);
                String str = sc.nextLine();
        int N = Integer.parseInt(str);

                long count=0;
        int prev_person = sc.nextInt();

        for(int i = 1; i < N; i++){
            int now_person = sc.nextInt();
            if(now_person < prev_person){
                count += prev_person - now_person;
            }
            else{
                prev_person = now_person;
            }
        }


                System.out.println(count);
                sc.close();
        }
} public class Main{
        public static void main(String[] args){
                Scanner sc = new Scanner(System.in);
                String str = sc.nextLine();
        int N = Integer.parseInt(str);

                long count=0;
        int prev_person = sc.nextInt();

        for(int i = 1; i < N; i++){
            int now_person = sc.nextInt();
            if(now_person < prev_person){
                count += prev_person - now_person;
            }
            else{
                prev_person = now_person;
            }
        }


                System.out.println(count);
                sc.close();
        }
} Main
import java.util.*;

public class Main{
        public static void main(String[] args){
                Scanner sc = new Scanner(System.in);
                String str = sc.nextLine();
        int N = Integer.parseInt(str);

                long count=0;
        int prev_person = sc.nextInt();

        for(int i = 1; i < N; i++){
            int now_person = sc.nextInt();
            if(now_person < prev_person){
                count += prev_person - now_person;
            }
            else{
                prev_person = now_person;
            }
        }


                System.out.println(count);
                sc.close();
        }
}

