import java.io.*;
import java.util.*;

public class Main{
    final static String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toLowerCase();//.toCharArray();
    static class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){br = new BufferedReader(new InputStreamReader(System.in));}
        String next(){while (st == null || !st.hasMoreElements()){try{st = new StringTokenizer(br.readLine());}catch (IOException  e){e.printStackTrace();}}return st.nextToken();}
        int nextInt(){return Integer.parseInt(next());}
        long nextLong(){return Long.parseLong(next());}
        double nextDouble(){return Double.parseDouble(next());}
        int[] readArray(int n){int[] a=new int[n];for (int i=0; i<n; i++) a[i]=nextInt();return a;}
        long[] readLongArray(int n){long[] a=new long[n];for (int i=0; i<n; i++) a[i]=nextLong();return a;}
        String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;}
    }
    static int getGcd(int n1,int n2){
        while(n1 != n2){
            if(n1 > n2) n1 -= n2;
            else n2 -= n1;
        }
        return n1;
    }
    static boolean isSorted(int[] arr, int n){
        for(int i=1;i<n;i++){
            if(arr[i]<arr[i-1]) return false;
        }
        return true;
    }
    static boolean isPrime(long n){
        if(n==2) return true;
        else if(n%2==0) return false;
        for(int i=3;i<=n/2;i+=2){
            if(n%i==0) return false;
        }
        return true;
    }
    static boolean isPrime(int n){
        if(n==2) return true;
        else if(n%2==0) return false;
        for(int i=3;i<=n/2;i+=2){
            if(n%i==0) return false;
        }
        return true;
    }
    static void printArray(int[] arr){
        for(int x:arr){
            System.out.print(x+" ");
        }
        System.out.println();
    }
    static long Fact(int n){
        //int p=1000000007;
        long result = 1;
        for (int i = 1; i <= n; i++) result = (result*i);
        //for (int i = 1; i <= n; i++) result = (result*i)%p;
        return result;
    }
    static final Random random=new Random();
    static void ruffleSort(int[] a) {
        int n=a.length;//shuffle, then sort
        for (int i=0; i<n; i++) {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        }
        Arrays.sort(a);
    }
    public static void main(String[] args) throws NumberFormatException, IOException {
        FastReader s = new FastReader();
        PrintWriter pw = new PrintWriter(System.out,true);
        int n=s.nextInt();
        int[] a = s.readArray(n);
        long sum=0;
        for(int i=1;i<n;i++){
            if(a[i]<a[i-1]){
                sum+=a[i-1]-a[i];a[i]=a[i-1];
            }
        }pw.println(sum);
    }
} import java.io.*; io.* java import java.util.*; util.* java public class Main{
    final static String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toLowerCase();//.toCharArray();
    static class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){br = new BufferedReader(new InputStreamReader(System.in));}
        String next(){while (st == null || !st.hasMoreElements()){try{st = new StringTokenizer(br.readLine());}catch (IOException  e){e.printStackTrace();}}return st.nextToken();}
        int nextInt(){return Integer.parseInt(next());}
        long nextLong(){return Long.parseLong(next());}
        double nextDouble(){return Double.parseDouble(next());}
        int[] readArray(int n){int[] a=new int[n];for (int i=0; i<n; i++) a[i]=nextInt();return a;}
        long[] readLongArray(int n){long[] a=new long[n];for (int i=0; i<n; i++) a[i]=nextLong();return a;}
        String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;}
    }
    static int getGcd(int n1,int n2){
        while(n1 != n2){
            if(n1 > n2) n1 -= n2;
            else n2 -= n1;
        }
        return n1;
    }
    static boolean isSorted(int[] arr, int n){
        for(int i=1;i<n;i++){
            if(arr[i]<arr[i-1]) return false;
        }
        return true;
    }
    static boolean isPrime(long n){
        if(n==2) return true;
        else if(n%2==0) return false;
        for(int i=3;i<=n/2;i+=2){
            if(n%i==0) return false;
        }
        return true;
    }
    static boolean isPrime(int n){
        if(n==2) return true;
        else if(n%2==0) return false;
        for(int i=3;i<=n/2;i+=2){
            if(n%i==0) return false;
        }
        return true;
    }
    static void printArray(int[] arr){
        for(int x:arr){
            System.out.print(x+" ");
        }
        System.out.println();
    }
    static long Fact(int n){
        //int p=1000000007;
        long result = 1;
        for (int i = 1; i <= n; i++) result = (result*i);
        //for (int i = 1; i <= n; i++) result = (result*i)%p;
        return result;
    }
    static final Random random=new Random();
    static void ruffleSort(int[] a) {
        int n=a.length;//shuffle, then sort
        for (int i=0; i<n; i++) {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        }
        Arrays.sort(a);
    }
    public static void main(String[] args) throws NumberFormatException, IOException {
        FastReader s = new FastReader();
        PrintWriter pw = new PrintWriter(System.out,true);
        int n=s.nextInt();
        int[] a = s.readArray(n);
        long sum=0;
        for(int i=1;i<n;i++){
            if(a[i]<a[i-1]){
                sum+=a[i-1]-a[i];a[i]=a[i-1];
            }
        }pw.println(sum);
    }
} Main final static String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toLowerCase();//.toCharArray(); alpha "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toLowerCase() "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toLowerCase "ABCDEFGHIJKLMNOPQRSTUVWXYZ" static final Random random=new Random(); random new Random() static int getGcd(int n1,int n2){
        while(n1 != n2){
            if(n1 > n2) n1 -= n2;
            else n2 -= n1;
        }
        return n1;
    } getGcd {
        while(n1 != n2){
            if(n1 > n2) n1 -= n2;
            else n2 -= n1;
        }
        return n1;
    } while(n1 != n2){
            if(n1 > n2) n1 -= n2;
            else n2 -= n1;
        } n1 != n2 n1 n2 {
            if(n1 > n2) n1 -= n2;
            else n2 -= n1;
        } if(n1 > n2) n1 -= n2;
            else n2 -= n1; n1 > n2 n1 n2 n1 -= n2 n1 n2 n2 -= n1 n2 n1 return n1; n1 int n1 n1 int n2 n2 static boolean isSorted(int[] arr, int n){
        for(int i=1;i<n;i++){
            if(arr[i]<arr[i-1]) return false;
        }
        return true;
    } isSorted {
        for(int i=1;i<n;i++){
            if(arr[i]<arr[i-1]) return false;
        }
        return true;
    } for(int i=1;i<n;i++){
            if(arr[i]<arr[i-1]) return false;
        } int i=1; int i=1; i 1 i<n i n i++ i++ i {
            if(arr[i]<arr[i-1]) return false;
        } {
            if(arr[i]<arr[i-1]) return false;
        } if(arr[i]<arr[i-1]) return false; arr[i]<arr[i-1] arr[i] arr i arr[i-1] arr i-1 i 1 return false; false return true; true int[] arr arr int n n static boolean isPrime(long n){
        if(n==2) return true;
        else if(n%2==0) return false;
        for(int i=3;i<=n/2;i+=2){
            if(n%i==0) return false;
        }
        return true;
    } isPrime {
        if(n==2) return true;
        else if(n%2==0) return false;
        for(int i=3;i<=n/2;i+=2){
            if(n%i==0) return false;
        }
        return true;
    } if(n==2) return true;
        else if(n%2==0) return false; n==2 n 2 return true; true if(n%2==0) return false; n%2==0 n%2 n 2 0 return false; false for(int i=3;i<=n/2;i+=2){
            if(n%i==0) return false;
        } int i=3; int i=3; i 3 i<=n/2 i n/2 n 2 i+=2 i+=2 i 2 {
            if(n%i==0) return false;
        } {
            if(n%i==0) return false;
        } if(n%i==0) return false; n%i==0 n%i n i 0 return false; false return true; true long n n static boolean isPrime(int n){
        if(n==2) return true;
        else if(n%2==0) return false;
        for(int i=3;i<=n/2;i+=2){
            if(n%i==0) return false;
        }
        return true;
    } isPrime {
        if(n==2) return true;
        else if(n%2==0) return false;
        for(int i=3;i<=n/2;i+=2){
            if(n%i==0) return false;
        }
        return true;
    } if(n==2) return true;
        else if(n%2==0) return false; n==2 n 2 return true; true if(n%2==0) return false; n%2==0 n%2 n 2 0 return false; false for(int i=3;i<=n/2;i+=2){
            if(n%i==0) return false;
        } int i=3; int i=3; i 3 i<=n/2 i n/2 n 2 i+=2 i+=2 i 2 {
            if(n%i==0) return false;
        } {
            if(n%i==0) return false;
        } if(n%i==0) return false; n%i==0 n%i n i 0 return false; false return true; true int n n static void printArray(int[] arr){
        for(int x:arr){
            System.out.print(x+" ");
        }
        System.out.println();
    } printArray {
        for(int x:arr){
            System.out.print(x+" ");
        }
        System.out.println();
    } for(int x:arr){
            System.out.print(x+" ");
        } int x arr {
            System.out.print(x+" ");
        } {
            System.out.print(x+" ");
        } System.out.print(x+" ") System.out.print System.out System System.out x+" " x " " System.out.println() System.out.println System.out System System.out int[] arr arr static long Fact(int n){
        //int p=1000000007;
        long result = 1;
        for (int i = 1; i <= n; i++) result = (result*i);
        //for (int i = 1; i <= n; i++) result = (result*i)%p;
        return result;
    } Fact {
        //int p=1000000007;
        long result = 1;
        for (int i = 1; i <= n; i++) result = (result*i);
        //for (int i = 1; i <= n; i++) result = (result*i)%p;
        return result;
    } long result = 1; result 1 for (int i = 1; i <= n; i++) result = (result*i); int i = 1; int i = 1; i 1 i <= n i n i++ i++ i result = (result*i); result = (result*i) result (result*i) result i return result; result int n n static void ruffleSort(int[] a) {
        int n=a.length;//shuffle, then sort
        for (int i=0; i<n; i++) {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        }
        Arrays.sort(a);
    } ruffleSort {
        int n=a.length;//shuffle, then sort
        for (int i=0; i<n; i++) {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        }
        Arrays.sort(a);
    } int n=a.length; n a.length a a.length for (int i=0; i<n; i++) {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        } int i=0; int i=0; i 0 i<n i n i++ i++ i {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        } {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        } int oi=random.nextInt(n) oi random.nextInt(n) random.nextInt random n temp=a[oi]; temp a[oi] a oi a[oi]=a[i] a[oi] a oi a[i] a i a[i]=temp a[i] a i temp Arrays.sort(a) Arrays.sort Arrays a int[] a a public static void main(String[] args) throws NumberFormatException, IOException {
        FastReader s = new FastReader();
        PrintWriter pw = new PrintWriter(System.out,true);
        int n=s.nextInt();
        int[] a = s.readArray(n);
        long sum=0;
        for(int i=1;i<n;i++){
            if(a[i]<a[i-1]){
                sum+=a[i-1]-a[i];a[i]=a[i-1];
            }
        }pw.println(sum);
    } main {
        FastReader s = new FastReader();
        PrintWriter pw = new PrintWriter(System.out,true);
        int n=s.nextInt();
        int[] a = s.readArray(n);
        long sum=0;
        for(int i=1;i<n;i++){
            if(a[i]<a[i-1]){
                sum+=a[i-1]-a[i];a[i]=a[i-1];
            }
        }pw.println(sum);
    } FastReader s = new FastReader(); s new FastReader() PrintWriter pw = new PrintWriter(System.out,true); pw new PrintWriter(System.out,true) int n=s.nextInt(); n s.nextInt() s.nextInt s int[] a = s.readArray(n); a s.readArray(n) s.readArray s n long sum=0; sum 0 for(int i=1;i<n;i++){
            if(a[i]<a[i-1]){
                sum+=a[i-1]-a[i];a[i]=a[i-1];
            }
        } int i=1; int i=1; i 1 i<n i n i++ i++ i {
            if(a[i]<a[i-1]){
                sum+=a[i-1]-a[i];a[i]=a[i-1];
            }
        } {
            if(a[i]<a[i-1]){
                sum+=a[i-1]-a[i];a[i]=a[i-1];
            }
        } if(a[i]<a[i-1]){
                sum+=a[i-1]-a[i];a[i]=a[i-1];
            } a[i]<a[i-1] a[i] a i a[i-1] a i-1 i 1 {
                sum+=a[i-1]-a[i];a[i]=a[i-1];
            } sum+=a[i-1]-a[i] sum a[i-1]-a[i] a[i-1] a i-1 i 1 a[i] a i a[i]=a[i-1] a[i] a i a[i-1] a i-1 i 1 pw.println(sum) pw.println pw sum String[] args args static class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){br = new BufferedReader(new InputStreamReader(System.in));}
        String next(){while (st == null || !st.hasMoreElements()){try{st = new StringTokenizer(br.readLine());}catch (IOException  e){e.printStackTrace();}}return st.nextToken();}
        int nextInt(){return Integer.parseInt(next());}
        long nextLong(){return Long.parseLong(next());}
        double nextDouble(){return Double.parseDouble(next());}
        int[] readArray(int n){int[] a=new int[n];for (int i=0; i<n; i++) a[i]=nextInt();return a;}
        long[] readLongArray(int n){long[] a=new long[n];for (int i=0; i<n; i++) a[i]=nextLong();return a;}
        String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;}
    } FastReader BufferedReader br; br StringTokenizer st; st public FastReader(){br = new BufferedReader(new InputStreamReader(System.in));} FastReader {br = new BufferedReader(new InputStreamReader(System.in));} br = new BufferedReader(new InputStreamReader(System.in)) br new BufferedReader(new InputStreamReader(System.in)) String next(){while (st == null || !st.hasMoreElements()){try{st = new StringTokenizer(br.readLine());}catch (IOException  e){e.printStackTrace();}}return st.nextToken();} next {while (st == null || !st.hasMoreElements()){try{st = new StringTokenizer(br.readLine());}catch (IOException  e){e.printStackTrace();}}return st.nextToken();} while (st == null || !st.hasMoreElements()){try{st = new StringTokenizer(br.readLine());}catch (IOException  e){e.printStackTrace();}} st == null || !st.hasMoreElements() st == null st null !st.hasMoreElements() st.hasMoreElements() st.hasMoreElements st {try{st = new StringTokenizer(br.readLine());}catch (IOException  e){e.printStackTrace();}} try{st = new StringTokenizer(br.readLine());}catch (IOException  e){e.printStackTrace();} catch (IOException  e){e.printStackTrace();} IOException  e {e.printStackTrace();} e.printStackTrace() e.printStackTrace 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 int[] readArray(int n){int[] a=new int[n];for (int i=0; i<n; i++) a[i]=nextInt();return a;} readArray {int[] a=new int[n];for (int i=0; i<n; i++) a[i]=nextInt();return a;} int[] a=new int[n]; a new int[n] n for (int i=0; i<n; i++) a[i]=nextInt(); int i=0; int i=0; i 0 i<n i n i++ i++ i a[i]=nextInt(); a[i]=nextInt() a[i] a i nextInt() nextInt return a; a int n n long[] readLongArray(int n){long[] a=new long[n];for (int i=0; i<n; i++) a[i]=nextLong();return a;} readLongArray {long[] a=new long[n];for (int i=0; i<n; i++) a[i]=nextLong();return a;} long[] a=new long[n]; a new long[n] n for (int i=0; i<n; i++) a[i]=nextLong(); int i=0; int i=0; i 0 i<n i n i++ i++ i a[i]=nextLong(); a[i]=nextLong() a[i] a i nextLong() nextLong return a; a int n n String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;} nextLine {String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;} String str = ""; str "" try{str = br.readLine();}catch (IOException e){e.printStackTrace();} catch (IOException e){e.printStackTrace();} IOException e {e.printStackTrace();} e.printStackTrace() e.printStackTrace e {str = br.readLine();} str = br.readLine() str br.readLine() br.readLine br return str; str public class Main{
    final static String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toLowerCase();//.toCharArray();
    static class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){br = new BufferedReader(new InputStreamReader(System.in));}
        String next(){while (st == null || !st.hasMoreElements()){try{st = new StringTokenizer(br.readLine());}catch (IOException  e){e.printStackTrace();}}return st.nextToken();}
        int nextInt(){return Integer.parseInt(next());}
        long nextLong(){return Long.parseLong(next());}
        double nextDouble(){return Double.parseDouble(next());}
        int[] readArray(int n){int[] a=new int[n];for (int i=0; i<n; i++) a[i]=nextInt();return a;}
        long[] readLongArray(int n){long[] a=new long[n];for (int i=0; i<n; i++) a[i]=nextLong();return a;}
        String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;}
    }
    static int getGcd(int n1,int n2){
        while(n1 != n2){
            if(n1 > n2) n1 -= n2;
            else n2 -= n1;
        }
        return n1;
    }
    static boolean isSorted(int[] arr, int n){
        for(int i=1;i<n;i++){
            if(arr[i]<arr[i-1]) return false;
        }
        return true;
    }
    static boolean isPrime(long n){
        if(n==2) return true;
        else if(n%2==0) return false;
        for(int i=3;i<=n/2;i+=2){
            if(n%i==0) return false;
        }
        return true;
    }
    static boolean isPrime(int n){
        if(n==2) return true;
        else if(n%2==0) return false;
        for(int i=3;i<=n/2;i+=2){
            if(n%i==0) return false;
        }
        return true;
    }
    static void printArray(int[] arr){
        for(int x:arr){
            System.out.print(x+" ");
        }
        System.out.println();
    }
    static long Fact(int n){
        //int p=1000000007;
        long result = 1;
        for (int i = 1; i <= n; i++) result = (result*i);
        //for (int i = 1; i <= n; i++) result = (result*i)%p;
        return result;
    }
    static final Random random=new Random();
    static void ruffleSort(int[] a) {
        int n=a.length;//shuffle, then sort
        for (int i=0; i<n; i++) {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        }
        Arrays.sort(a);
    }
    public static void main(String[] args) throws NumberFormatException, IOException {
        FastReader s = new FastReader();
        PrintWriter pw = new PrintWriter(System.out,true);
        int n=s.nextInt();
        int[] a = s.readArray(n);
        long sum=0;
        for(int i=1;i<n;i++){
            if(a[i]<a[i-1]){
                sum+=a[i-1]-a[i];a[i]=a[i-1];
            }
        }pw.println(sum);
    }
} public class Main{
    final static String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toLowerCase();//.toCharArray();
    static class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){br = new BufferedReader(new InputStreamReader(System.in));}
        String next(){while (st == null || !st.hasMoreElements()){try{st = new StringTokenizer(br.readLine());}catch (IOException  e){e.printStackTrace();}}return st.nextToken();}
        int nextInt(){return Integer.parseInt(next());}
        long nextLong(){return Long.parseLong(next());}
        double nextDouble(){return Double.parseDouble(next());}
        int[] readArray(int n){int[] a=new int[n];for (int i=0; i<n; i++) a[i]=nextInt();return a;}
        long[] readLongArray(int n){long[] a=new long[n];for (int i=0; i<n; i++) a[i]=nextLong();return a;}
        String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;}
    }
    static int getGcd(int n1,int n2){
        while(n1 != n2){
            if(n1 > n2) n1 -= n2;
            else n2 -= n1;
        }
        return n1;
    }
    static boolean isSorted(int[] arr, int n){
        for(int i=1;i<n;i++){
            if(arr[i]<arr[i-1]) return false;
        }
        return true;
    }
    static boolean isPrime(long n){
        if(n==2) return true;
        else if(n%2==0) return false;
        for(int i=3;i<=n/2;i+=2){
            if(n%i==0) return false;
        }
        return true;
    }
    static boolean isPrime(int n){
        if(n==2) return true;
        else if(n%2==0) return false;
        for(int i=3;i<=n/2;i+=2){
            if(n%i==0) return false;
        }
        return true;
    }
    static void printArray(int[] arr){
        for(int x:arr){
            System.out.print(x+" ");
        }
        System.out.println();
    }
    static long Fact(int n){
        //int p=1000000007;
        long result = 1;
        for (int i = 1; i <= n; i++) result = (result*i);
        //for (int i = 1; i <= n; i++) result = (result*i)%p;
        return result;
    }
    static final Random random=new Random();
    static void ruffleSort(int[] a) {
        int n=a.length;//shuffle, then sort
        for (int i=0; i<n; i++) {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        }
        Arrays.sort(a);
    }
    public static void main(String[] args) throws NumberFormatException, IOException {
        FastReader s = new FastReader();
        PrintWriter pw = new PrintWriter(System.out,true);
        int n=s.nextInt();
        int[] a = s.readArray(n);
        long sum=0;
        for(int i=1;i<n;i++){
            if(a[i]<a[i-1]){
                sum+=a[i-1]-a[i];a[i]=a[i-1];
            }
        }pw.println(sum);
    }
} Main
import java.io.*;
import java.util.*;

public class Main{
    final static String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toLowerCase();//.toCharArray();
    static class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){br = new BufferedReader(new InputStreamReader(System.in));}
        String next(){while (st == null || !st.hasMoreElements()){try{st = new StringTokenizer(br.readLine());}catch (IOException  e){e.printStackTrace();}}return st.nextToken();}
        int nextInt(){return Integer.parseInt(next());}
        long nextLong(){return Long.parseLong(next());}
        double nextDouble(){return Double.parseDouble(next());}
        int[] readArray(int n){int[] a=new int[n];for (int i=0; i<n; i++) a[i]=nextInt();return a;}
        long[] readLongArray(int n){long[] a=new long[n];for (int i=0; i<n; i++) a[i]=nextLong();return a;}
        String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;}
    }
    static int getGcd(int n1,int n2){
        while(n1 != n2){
            if(n1 > n2) n1 -= n2;
            else n2 -= n1;
        }
        return n1;
    }
    static boolean isSorted(int[] arr, int n){
        for(int i=1;i<n;i++){
            if(arr[i]<arr[i-1]) return false;
        }
        return true;
    }
    static boolean isPrime(long n){
        if(n==2) return true;
        else if(n%2==0) return false;
        for(int i=3;i<=n/2;i+=2){
            if(n%i==0) return false;
        }
        return true;
    }
    static boolean isPrime(int n){
        if(n==2) return true;
        else if(n%2==0) return false;
        for(int i=3;i<=n/2;i+=2){
            if(n%i==0) return false;
        }
        return true;
    }
    static void printArray(int[] arr){
        for(int x:arr){
            System.out.print(x+" ");
        }
        System.out.println();
    }
    static long Fact(int n){
        //int p=1000000007;
        long result = 1;
        for (int i = 1; i <= n; i++) result = (result*i);
        //for (int i = 1; i <= n; i++) result = (result*i)%p;
        return result;
    }
    static final Random random=new Random();
    static void ruffleSort(int[] a) {
        int n=a.length;//shuffle, then sort
        for (int i=0; i<n; i++) {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        }
        Arrays.sort(a);
    }
    public static void main(String[] args) throws NumberFormatException, IOException {
        FastReader s = new FastReader();
        PrintWriter pw = new PrintWriter(System.out,true);
        int n=s.nextInt();
        int[] a = s.readArray(n);
        long sum=0;
        for(int i=1;i<n;i++){
            if(a[i]<a[i-1]){
                sum+=a[i-1]-a[i];a[i]=a[i-1];
            }
        }pw.println(sum);
    }
}