//package com.company;
import java.util.Scanner;
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import static java.lang.Math.*;

public class Main {

    static class FastScanner {
        StringTokenizer tok = new StringTokenizer("");
        BufferedReader in;

        FastScanner()  {
            in = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() throws IOException {
            while (!tok.hasMoreElements())
                tok = new StringTokenizer(in.readLine());
            return tok.nextToken();
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }


    }

    public static void main(String[] args) throws IOException {

        FastScanner input = new FastScanner();
        Scanner in = new Scanner(System.in);

           int n = input.nextInt();
           int a[] = new int[n];
           long ans = 0 ;
           for( int i = 0 ; i < n ; i++ ){
               a[i] = input.nextInt();
               if( i > 0 ){
                   if( a[i] < a[i-1]){
                       ans += Math.abs(a[i]-a[i-1]);
                       a[i] = a[i-1];
                   }
               }
           }
           System.out.println(ans);




    }
} import java.util.Scanner; Scanner java.util import java.util.*; util.* java import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io import static java.lang.Math.*; import static java.lang.Math.*; import static java.lang.Math.*; public class Main {

    static class FastScanner {
        StringTokenizer tok = new StringTokenizer("");
        BufferedReader in;

        FastScanner()  {
            in = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() throws IOException {
            while (!tok.hasMoreElements())
                tok = new StringTokenizer(in.readLine());
            return tok.nextToken();
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }


    }

    public static void main(String[] args) throws IOException {

        FastScanner input = new FastScanner();
        Scanner in = new Scanner(System.in);

           int n = input.nextInt();
           int a[] = new int[n];
           long ans = 0 ;
           for( int i = 0 ; i < n ; i++ ){
               a[i] = input.nextInt();
               if( i > 0 ){
                   if( a[i] < a[i-1]){
                       ans += Math.abs(a[i]-a[i-1]);
                       a[i] = a[i-1];
                   }
               }
           }
           System.out.println(ans);




    }
} Main public static void main(String[] args) throws IOException {

        FastScanner input = new FastScanner();
        Scanner in = new Scanner(System.in);

           int n = input.nextInt();
           int a[] = new int[n];
           long ans = 0 ;
           for( int i = 0 ; i < n ; i++ ){
               a[i] = input.nextInt();
               if( i > 0 ){
                   if( a[i] < a[i-1]){
                       ans += Math.abs(a[i]-a[i-1]);
                       a[i] = a[i-1];
                   }
               }
           }
           System.out.println(ans);




    } main {

        FastScanner input = new FastScanner();
        Scanner in = new Scanner(System.in);

           int n = input.nextInt();
           int a[] = new int[n];
           long ans = 0 ;
           for( int i = 0 ; i < n ; i++ ){
               a[i] = input.nextInt();
               if( i > 0 ){
                   if( a[i] < a[i-1]){
                       ans += Math.abs(a[i]-a[i-1]);
                       a[i] = a[i-1];
                   }
               }
           }
           System.out.println(ans);




    } FastScanner input = new FastScanner(); input new FastScanner() Scanner in = new Scanner(System.in); in new Scanner(System.in) int n = input.nextInt(); n input.nextInt() input.nextInt input int a[] = new int[n]; a new int[n] n long ans = 0 ; ans 0 for( int i = 0 ; i < n ; i++ ){
               a[i] = input.nextInt();
               if( i > 0 ){
                   if( a[i] < a[i-1]){
                       ans += Math.abs(a[i]-a[i-1]);
                       a[i] = a[i-1];
                   }
               }
           } int i = 0 ; int i = 0 ; i 0 i < n i n i++ i++ i {
               a[i] = input.nextInt();
               if( i > 0 ){
                   if( a[i] < a[i-1]){
                       ans += Math.abs(a[i]-a[i-1]);
                       a[i] = a[i-1];
                   }
               }
           } {
               a[i] = input.nextInt();
               if( i > 0 ){
                   if( a[i] < a[i-1]){
                       ans += Math.abs(a[i]-a[i-1]);
                       a[i] = a[i-1];
                   }
               }
           } a[i] = input.nextInt() a[i] a i input.nextInt() input.nextInt input if( i > 0 ){
                   if( a[i] < a[i-1]){
                       ans += Math.abs(a[i]-a[i-1]);
                       a[i] = a[i-1];
                   }
               } i > 0 i 0 {
                   if( a[i] < a[i-1]){
                       ans += Math.abs(a[i]-a[i-1]);
                       a[i] = a[i-1];
                   }
               } if( a[i] < a[i-1]){
                       ans += Math.abs(a[i]-a[i-1]);
                       a[i] = a[i-1];
                   } a[i] < a[i-1] a[i] a i a[i-1] a i-1 i 1 {
                       ans += Math.abs(a[i]-a[i-1]);
                       a[i] = a[i-1];
                   } ans += Math.abs(a[i]-a[i-1]) ans Math.abs(a[i]-a[i-1]) Math.abs Math a[i]-a[i-1] a[i] a i a[i-1] a i-1 i 1 a[i] = a[i-1] a[i] a i a[i-1] a i-1 i 1 System.out.println(ans) System.out.println System.out System System.out ans String[] args args static class FastScanner {
        StringTokenizer tok = new StringTokenizer("");
        BufferedReader in;

        FastScanner()  {
            in = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() throws IOException {
            while (!tok.hasMoreElements())
                tok = new StringTokenizer(in.readLine());
            return tok.nextToken();
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }


    } FastScanner StringTokenizer tok = new StringTokenizer(""); tok new StringTokenizer("") BufferedReader in; in FastScanner()  {
            in = new BufferedReader(new InputStreamReader(System.in));
        } FastScanner {
            in = new BufferedReader(new InputStreamReader(System.in));
        } in = new BufferedReader(new InputStreamReader(System.in)) in new BufferedReader(new InputStreamReader(System.in)) String next() throws IOException {
            while (!tok.hasMoreElements())
                tok = new StringTokenizer(in.readLine());
            return tok.nextToken();
        } next {
            while (!tok.hasMoreElements())
                tok = new StringTokenizer(in.readLine());
            return tok.nextToken();
        } while (!tok.hasMoreElements())
                tok = new StringTokenizer(in.readLine()); !tok.hasMoreElements() tok.hasMoreElements() tok.hasMoreElements tok tok = new StringTokenizer(in.readLine()) tok new StringTokenizer(in.readLine()) return tok.nextToken(); tok.nextToken() tok.nextToken tok int nextInt() throws IOException {
            return Integer.parseInt(next());
        } nextInt {
            return Integer.parseInt(next());
        } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next public class Main {

    static class FastScanner {
        StringTokenizer tok = new StringTokenizer("");
        BufferedReader in;

        FastScanner()  {
            in = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() throws IOException {
            while (!tok.hasMoreElements())
                tok = new StringTokenizer(in.readLine());
            return tok.nextToken();
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }


    }

    public static void main(String[] args) throws IOException {

        FastScanner input = new FastScanner();
        Scanner in = new Scanner(System.in);

           int n = input.nextInt();
           int a[] = new int[n];
           long ans = 0 ;
           for( int i = 0 ; i < n ; i++ ){
               a[i] = input.nextInt();
               if( i > 0 ){
                   if( a[i] < a[i-1]){
                       ans += Math.abs(a[i]-a[i-1]);
                       a[i] = a[i-1];
                   }
               }
           }
           System.out.println(ans);




    }
} public class Main {

    static class FastScanner {
        StringTokenizer tok = new StringTokenizer("");
        BufferedReader in;

        FastScanner()  {
            in = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() throws IOException {
            while (!tok.hasMoreElements())
                tok = new StringTokenizer(in.readLine());
            return tok.nextToken();
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }


    }

    public static void main(String[] args) throws IOException {

        FastScanner input = new FastScanner();
        Scanner in = new Scanner(System.in);

           int n = input.nextInt();
           int a[] = new int[n];
           long ans = 0 ;
           for( int i = 0 ; i < n ; i++ ){
               a[i] = input.nextInt();
               if( i > 0 ){
                   if( a[i] < a[i-1]){
                       ans += Math.abs(a[i]-a[i-1]);
                       a[i] = a[i-1];
                   }
               }
           }
           System.out.println(ans);




    }
} Main
//package com.company;
import java.util.Scanner;
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import static java.lang.Math.*;

public class Main {

    static class FastScanner {
        StringTokenizer tok = new StringTokenizer("");
        BufferedReader in;

        FastScanner()  {
            in = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() throws IOException {
            while (!tok.hasMoreElements())
                tok = new StringTokenizer(in.readLine());
            return tok.nextToken();
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }


    }

    public static void main(String[] args) throws IOException {

        FastScanner input = new FastScanner();
        Scanner in = new Scanner(System.in);

           int n = input.nextInt();
           int a[] = new int[n];
           long ans = 0 ;
           for( int i = 0 ; i < n ; i++ ){
               a[i] = input.nextInt();
               if( i > 0 ){
                   if( a[i] < a[i-1]){
                       ans += Math.abs(a[i]-a[i-1]);
                       a[i] = a[i-1];
                   }
               }
           }
           System.out.println(ans);




    }
}