import java.util.*;
import java.io.*;
import java.util.Arrays; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.StringTokenizer;
import java.math.BigInteger;

public class Main {
    



    public static void main(String[] args) {
        try (PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out))) {
            FastScanner in = new FastScanner(System.in);
            int n = in.nextInt();
            long[] array = in.nextLongArray(n);
            long count = 0;

            for(int i = 1; i < n; i++) {
                if(array[i] < array[i-1]) {
                    count += array[i-1] - array[i];
                    array[i] = array[i-1];
                }
            }

            out.println(count);



        } finally {
            //out.close();
        }
        
           
    }
}




class FastScanner {
   private final BufferedReader br;
   private StringTokenizer st;
 
    FastScanner(InputStream InputStream) {
        br = new BufferedReader(new InputStreamReader(InputStream));
    }
    
    String next() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return st.nextToken();
    }
    
    int nextInt() {
        return Integer.parseInt(next());
    }
    
    long nextLong() {
        return Long.parseLong(next());
    }
    
    double nextDouble() {
        return Double.parseDouble(next());
    }


    
    ArrayList<Integer> nextIntList(int count) {
        ArrayList<Integer> array = new ArrayList<>();
        for (int n = 0; n < count; n++) {
            int number = nextInt();
            array.add(number);
        }
        return array;
    }
    
    int[] nextIntArray(int count) {
        int[] array = new int[count];
        for (int n = 0; n < count; n++) {
            array[n] = nextInt();
        }
        return array;
    }

    long[] nextLongArray(int count) {
        long[] array = new long[count];
        for (int n = 0; n < count; n++) {
            array[n] = nextLong();
        }
        return array;
    }
} import java.util.*; util.* java import java.io.*; io.* java import java.util.Arrays; Arrays java.util import java.util.ArrayList; ArrayList java.util import java.util.Collections; Collections java.util import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io import java.io.InputStream; InputStream java.io import java.util.StringTokenizer; StringTokenizer java.util import java.math.BigInteger; BigInteger java.math public class Main {
    



    public static void main(String[] args) {
        try (PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out))) {
            FastScanner in = new FastScanner(System.in);
            int n = in.nextInt();
            long[] array = in.nextLongArray(n);
            long count = 0;

            for(int i = 1; i < n; i++) {
                if(array[i] < array[i-1]) {
                    count += array[i-1] - array[i];
                    array[i] = array[i-1];
                }
            }

            out.println(count);



        } finally {
            //out.close();
        }
        
           
    }
} Main public static void main(String[] args) {
        try (PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out))) {
            FastScanner in = new FastScanner(System.in);
            int n = in.nextInt();
            long[] array = in.nextLongArray(n);
            long count = 0;

            for(int i = 1; i < n; i++) {
                if(array[i] < array[i-1]) {
                    count += array[i-1] - array[i];
                    array[i] = array[i-1];
                }
            }

            out.println(count);



        } finally {
            //out.close();
        }
        
           
    } main {
        try (PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out))) {
            FastScanner in = new FastScanner(System.in);
            int n = in.nextInt();
            long[] array = in.nextLongArray(n);
            long count = 0;

            for(int i = 1; i < n; i++) {
                if(array[i] < array[i-1]) {
                    count += array[i-1] - array[i];
                    array[i] = array[i-1];
                }
            }

            out.println(count);



        } finally {
            //out.close();
        }
        
           
    } try (PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out))) {
            FastScanner in = new FastScanner(System.in);
            int n = in.nextInt();
            long[] array = in.nextLongArray(n);
            long count = 0;

            for(int i = 1; i < n; i++) {
                if(array[i] < array[i-1]) {
                    count += array[i-1] - array[i];
                    array[i] = array[i-1];
                }
            }

            out.println(count);



        } finally {
            //out.close();
        } {
            //out.close();
        } {
            FastScanner in = new FastScanner(System.in);
            int n = in.nextInt();
            long[] array = in.nextLongArray(n);
            long count = 0;

            for(int i = 1; i < n; i++) {
                if(array[i] < array[i-1]) {
                    count += array[i-1] - array[i];
                    array[i] = array[i-1];
                }
            }

            out.println(count);



        } PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)) PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)) new PrintWriter(new BufferedOutputStream(System.out)) FastScanner in = new FastScanner(System.in); in new FastScanner(System.in) int n = in.nextInt(); n in.nextInt() in.nextInt in long[] array = in.nextLongArray(n); array in.nextLongArray(n) in.nextLongArray in n long count = 0; count 0 for(int i = 1; i < n; i++) {
                if(array[i] < array[i-1]) {
                    count += array[i-1] - array[i];
                    array[i] = array[i-1];
                }
            } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
                if(array[i] < array[i-1]) {
                    count += array[i-1] - array[i];
                    array[i] = array[i-1];
                }
            } {
                if(array[i] < array[i-1]) {
                    count += array[i-1] - array[i];
                    array[i] = array[i-1];
                }
            } if(array[i] < array[i-1]) {
                    count += array[i-1] - array[i];
                    array[i] = array[i-1];
                } array[i] < array[i-1] array[i] array i array[i-1] array i-1 i 1 {
                    count += array[i-1] - array[i];
                    array[i] = array[i-1];
                } count += array[i-1] - array[i] count array[i-1] - array[i] array[i-1] array i-1 i 1 array[i] array i array[i] = array[i-1] array[i] array i array[i-1] array i-1 i 1 out.println(count) out.println out count String[] args args class FastScanner {
   private final BufferedReader br;
   private StringTokenizer st;
 
    FastScanner(InputStream InputStream) {
        br = new BufferedReader(new InputStreamReader(InputStream));
    }
    
    String next() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return st.nextToken();
    }
    
    int nextInt() {
        return Integer.parseInt(next());
    }
    
    long nextLong() {
        return Long.parseLong(next());
    }
    
    double nextDouble() {
        return Double.parseDouble(next());
    }


    
    ArrayList<Integer> nextIntList(int count) {
        ArrayList<Integer> array = new ArrayList<>();
        for (int n = 0; n < count; n++) {
            int number = nextInt();
            array.add(number);
        }
        return array;
    }
    
    int[] nextIntArray(int count) {
        int[] array = new int[count];
        for (int n = 0; n < count; n++) {
            array[n] = nextInt();
        }
        return array;
    }

    long[] nextLongArray(int count) {
        long[] array = new long[count];
        for (int n = 0; n < count; n++) {
            array[n] = nextLong();
        }
        return array;
    }
} FastScanner private final BufferedReader br; br private StringTokenizer st; st FastScanner(InputStream InputStream) {
        br = new BufferedReader(new InputStreamReader(InputStream));
    } FastScanner {
        br = new BufferedReader(new InputStreamReader(InputStream));
    } br = new BufferedReader(new InputStreamReader(InputStream)) br new BufferedReader(new InputStreamReader(InputStream)) InputStream InputStream InputStream String next() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return st.nextToken();
    } next {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return st.nextToken();
    } while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } st == null || !st.hasMoreTokens() st == null st null !st.hasMoreTokens() st.hasMoreTokens() st.hasMoreTokens st {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            } IOException e {
                throw new RuntimeException(e);
            } throw new RuntimeException(e); new RuntimeException(e) {
                st = new StringTokenizer(br.readLine());
            } st = new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) return st.nextToken(); st.nextToken() st.nextToken st int nextInt() {
        return Integer.parseInt(next());
    } nextInt {
        return Integer.parseInt(next());
    } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next long nextLong() {
        return Long.parseLong(next());
    } nextLong {
        return Long.parseLong(next());
    } return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next double nextDouble() {
        return Double.parseDouble(next());
    } nextDouble {
        return Double.parseDouble(next());
    } return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next ArrayList<Integer> nextIntList(int count) {
        ArrayList<Integer> array = new ArrayList<>();
        for (int n = 0; n < count; n++) {
            int number = nextInt();
            array.add(number);
        }
        return array;
    } nextIntList {
        ArrayList<Integer> array = new ArrayList<>();
        for (int n = 0; n < count; n++) {
            int number = nextInt();
            array.add(number);
        }
        return array;
    } ArrayList<Integer> array = new ArrayList<>(); array new ArrayList<>() for (int n = 0; n < count; n++) {
            int number = nextInt();
            array.add(number);
        } int n = 0; int n = 0; n 0 n < count n count n++ n++ n {
            int number = nextInt();
            array.add(number);
        } {
            int number = nextInt();
            array.add(number);
        } int number = nextInt(); number nextInt() nextInt array.add(number) array.add array number return array; array int count count int[] nextIntArray(int count) {
        int[] array = new int[count];
        for (int n = 0; n < count; n++) {
            array[n] = nextInt();
        }
        return array;
    } nextIntArray {
        int[] array = new int[count];
        for (int n = 0; n < count; n++) {
            array[n] = nextInt();
        }
        return array;
    } int[] array = new int[count]; array new int[count] count for (int n = 0; n < count; n++) {
            array[n] = nextInt();
        } int n = 0; int n = 0; n 0 n < count n count n++ n++ n {
            array[n] = nextInt();
        } {
            array[n] = nextInt();
        } array[n] = nextInt() array[n] array n nextInt() nextInt return array; array int count count long[] nextLongArray(int count) {
        long[] array = new long[count];
        for (int n = 0; n < count; n++) {
            array[n] = nextLong();
        }
        return array;
    } nextLongArray {
        long[] array = new long[count];
        for (int n = 0; n < count; n++) {
            array[n] = nextLong();
        }
        return array;
    } long[] array = new long[count]; array new long[count] count for (int n = 0; n < count; n++) {
            array[n] = nextLong();
        } int n = 0; int n = 0; n 0 n < count n count n++ n++ n {
            array[n] = nextLong();
        } {
            array[n] = nextLong();
        } array[n] = nextLong() array[n] array n nextLong() nextLong return array; array int count count public class Main {
    



    public static void main(String[] args) {
        try (PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out))) {
            FastScanner in = new FastScanner(System.in);
            int n = in.nextInt();
            long[] array = in.nextLongArray(n);
            long count = 0;

            for(int i = 1; i < n; i++) {
                if(array[i] < array[i-1]) {
                    count += array[i-1] - array[i];
                    array[i] = array[i-1];
                }
            }

            out.println(count);



        } finally {
            //out.close();
        }
        
           
    }
} public class Main {
    



    public static void main(String[] args) {
        try (PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out))) {
            FastScanner in = new FastScanner(System.in);
            int n = in.nextInt();
            long[] array = in.nextLongArray(n);
            long count = 0;

            for(int i = 1; i < n; i++) {
                if(array[i] < array[i-1]) {
                    count += array[i-1] - array[i];
                    array[i] = array[i-1];
                }
            }

            out.println(count);



        } finally {
            //out.close();
        }
        
           
    }
} Main
import java.util.*;
import java.io.*;
import java.util.Arrays; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.StringTokenizer;
import java.math.BigInteger;

public class Main {
    



    public static void main(String[] args) {
        try (PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out))) {
            FastScanner in = new FastScanner(System.in);
            int n = in.nextInt();
            long[] array = in.nextLongArray(n);
            long count = 0;

            for(int i = 1; i < n; i++) {
                if(array[i] < array[i-1]) {
                    count += array[i-1] - array[i];
                    array[i] = array[i-1];
                }
            }

            out.println(count);



        } finally {
            //out.close();
        }
        
           
    }
}




class FastScanner {
   private final BufferedReader br;
   private StringTokenizer st;
 
    FastScanner(InputStream InputStream) {
        br = new BufferedReader(new InputStreamReader(InputStream));
    }
    
    String next() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return st.nextToken();
    }
    
    int nextInt() {
        return Integer.parseInt(next());
    }
    
    long nextLong() {
        return Long.parseLong(next());
    }
    
    double nextDouble() {
        return Double.parseDouble(next());
    }


    
    ArrayList<Integer> nextIntList(int count) {
        ArrayList<Integer> array = new ArrayList<>();
        for (int n = 0; n < count; n++) {
            int number = nextInt();
            array.add(number);
        }
        return array;
    }
    
    int[] nextIntArray(int count) {
        int[] array = new int[count];
        for (int n = 0; n < count; n++) {
            array[n] = nextInt();
        }
        return array;
    }

    long[] nextLongArray(int count) {
        long[] array = new long[count];
        for (int n = 0; n < count; n++) {
            array[n] = nextLong();
        }
        return array;
    }
}
