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


// A1 * Sum(A2+...+An)
// +A2 * Sum(A3+...+An)
// +A3 * Sum(A4+...+An)
public class Main {
    static StringTokenizer st;
    static BufferedReader br;  
    static PrintWriter out;
    
    static long mod = (long)1e9 + 7;

    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        int N = nextInt();
   
        long ans = 0;
        int prev =  nextInt();
        // we get PreSum
        for (int i=2; i<=N; i++) {
            int next = nextInt();
            if(next<prev){
                ans += prev-next;
            }
            else{
                prev = next;
            }
        }

        System.out.println(ans);
    }

      
    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
} import java.util.*; util.* java import java.io.*; io.* java public class Main {
    static StringTokenizer st;
    static BufferedReader br;  
    static PrintWriter out;
    
    static long mod = (long)1e9 + 7;

    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        int N = nextInt();
   
        long ans = 0;
        int prev =  nextInt();
        // we get PreSum
        for (int i=2; i<=N; i++) {
            int next = nextInt();
            if(next<prev){
                ans += prev-next;
            }
            else{
                prev = next;
            }
        }

        System.out.println(ans);
    }

      
    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
} Main static StringTokenizer st; st static BufferedReader br; br static PrintWriter out; out static long mod = (long)1e9 + 7; mod (long)1e9 + 7 (long)1e9 7 public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        int N = nextInt();
   
        long ans = 0;
        int prev =  nextInt();
        // we get PreSum
        for (int i=2; i<=N; i++) {
            int next = nextInt();
            if(next<prev){
                ans += prev-next;
            }
            else{
                prev = next;
            }
        }

        System.out.println(ans);
    } main {
        br = new BufferedReader(new InputStreamReader(System.in));
        int N = nextInt();
   
        long ans = 0;
        int prev =  nextInt();
        // we get PreSum
        for (int i=2; i<=N; i++) {
            int next = nextInt();
            if(next<prev){
                ans += prev-next;
            }
            else{
                prev = next;
            }
        }

        System.out.println(ans);
    } br = new BufferedReader(new InputStreamReader(System.in)) br new BufferedReader(new InputStreamReader(System.in)) int N = nextInt(); N nextInt() nextInt long ans = 0; ans 0 int prev =  nextInt(); prev nextInt() nextInt for (int i=2; i<=N; i++) {
            int next = nextInt();
            if(next<prev){
                ans += prev-next;
            }
            else{
                prev = next;
            }
        } int i=2; int i=2; i 2 i<=N i N i++ i++ i {
            int next = nextInt();
            if(next<prev){
                ans += prev-next;
            }
            else{
                prev = next;
            }
        } {
            int next = nextInt();
            if(next<prev){
                ans += prev-next;
            }
            else{
                prev = next;
            }
        } int next = nextInt(); next nextInt() nextInt if(next<prev){
                ans += prev-next;
            }
            else{
                prev = next;
            } next<prev next prev {
                ans += prev-next;
            } ans += prev-next ans prev-next prev next {
                prev = next;
            } prev = next prev next System.out.println(ans) System.out.println System.out System System.out ans String[] args args public static 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 static long nextLong() throws IOException {
        return Long.parseLong(next());
    } nextLong {
        return Long.parseLong(next());
    } return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    } nextDouble {
        return Double.parseDouble(next());
    } return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    } next {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    } while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim()); st == null || !st.hasMoreTokens() st == null st null !st.hasMoreTokens() st.hasMoreTokens() st.hasMoreTokens st st = new StringTokenizer(br.readLine().trim()) st new StringTokenizer(br.readLine().trim()) return st.nextToken(); st.nextToken() st.nextToken st public class Main {
    static StringTokenizer st;
    static BufferedReader br;  
    static PrintWriter out;
    
    static long mod = (long)1e9 + 7;

    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        int N = nextInt();
   
        long ans = 0;
        int prev =  nextInt();
        // we get PreSum
        for (int i=2; i<=N; i++) {
            int next = nextInt();
            if(next<prev){
                ans += prev-next;
            }
            else{
                prev = next;
            }
        }

        System.out.println(ans);
    }

      
    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
} public class Main {
    static StringTokenizer st;
    static BufferedReader br;  
    static PrintWriter out;
    
    static long mod = (long)1e9 + 7;

    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        int N = nextInt();
   
        long ans = 0;
        int prev =  nextInt();
        // we get PreSum
        for (int i=2; i<=N; i++) {
            int next = nextInt();
            if(next<prev){
                ans += prev-next;
            }
            else{
                prev = next;
            }
        }

        System.out.println(ans);
    }

      
    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
} Main
import java.util.*; 
import java.io.*;


// A1 * Sum(A2+...+An)
// +A2 * Sum(A3+...+An)
// +A3 * Sum(A4+...+An)
public class Main {
    static StringTokenizer st;
    static BufferedReader br;  
    static PrintWriter out;
    
    static long mod = (long)1e9 + 7;

    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        int N = nextInt();
   
        long ans = 0;
        int prev =  nextInt();
        // we get PreSum
        for (int i=2; i<=N; i++) {
            int next = nextInt();
            if(next<prev){
                ans += prev-next;
            }
            else{
                prev = next;
            }
        }

        System.out.println(ans);
    }

      
    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
}