// package Contest#176;
import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);

        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        StringTokenizer st1 = new StringTokenizer(br.readLine());

        long sum = 0;
        int max = Integer.MIN_VALUE;

        for(int i=1; i<=n; i++){
            int ht = Integer.parseInt(st1.nextToken());
            max = Math.max(max, ht);
            if(ht < max){
                sum += (max-ht);
            }
        }

        out.println(sum);
        out.close();
    }
} import java.util.*; util.* java import java.io.*; io.* java public class Main {
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);

        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        StringTokenizer st1 = new StringTokenizer(br.readLine());

        long sum = 0;
        int max = Integer.MIN_VALUE;

        for(int i=1; i<=n; i++){
            int ht = Integer.parseInt(st1.nextToken());
            max = Math.max(max, ht);
            if(ht < max){
                sum += (max-ht);
            }
        }

        out.println(sum);
        out.close();
    }
} Main public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);

        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        StringTokenizer st1 = new StringTokenizer(br.readLine());

        long sum = 0;
        int max = Integer.MIN_VALUE;

        for(int i=1; i<=n; i++){
            int ht = Integer.parseInt(st1.nextToken());
            max = Math.max(max, ht);
            if(ht < max){
                sum += (max-ht);
            }
        }

        out.println(sum);
        out.close();
    } main {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);

        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        StringTokenizer st1 = new StringTokenizer(br.readLine());

        long sum = 0;
        int max = Integer.MIN_VALUE;

        for(int i=1; i<=n; i++){
            int ht = Integer.parseInt(st1.nextToken());
            max = Math.max(max, ht);
            if(ht < max){
                sum += (max-ht);
            }
        }

        out.println(sum);
        out.close();
    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) PrintWriter out = new PrintWriter(System.out); out new PrintWriter(System.out) StringTokenizer st = new StringTokenizer(br.readLine()); st new StringTokenizer(br.readLine()) int n = Integer.parseInt(st.nextToken()); n Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st StringTokenizer st1 = new StringTokenizer(br.readLine()); st1 new StringTokenizer(br.readLine()) long sum = 0; sum 0 int max = Integer.MIN_VALUE; max Integer.MIN_VALUE Integer Integer.MIN_VALUE for(int i=1; i<=n; i++){
            int ht = Integer.parseInt(st1.nextToken());
            max = Math.max(max, ht);
            if(ht < max){
                sum += (max-ht);
            }
        } int i=1; int i=1; i 1 i<=n i n i++ i++ i {
            int ht = Integer.parseInt(st1.nextToken());
            max = Math.max(max, ht);
            if(ht < max){
                sum += (max-ht);
            }
        } {
            int ht = Integer.parseInt(st1.nextToken());
            max = Math.max(max, ht);
            if(ht < max){
                sum += (max-ht);
            }
        } int ht = Integer.parseInt(st1.nextToken()); ht Integer.parseInt(st1.nextToken()) Integer.parseInt Integer st1.nextToken() st1.nextToken st1 max = Math.max(max, ht) max Math.max(max, ht) Math.max Math max ht if(ht < max){
                sum += (max-ht);
            } ht < max ht max {
                sum += (max-ht);
            } sum += (max-ht) sum (max-ht) max ht out.println(sum) out.println out sum out.close() out.close out String[] args args public class Main {
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);

        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        StringTokenizer st1 = new StringTokenizer(br.readLine());

        long sum = 0;
        int max = Integer.MIN_VALUE;

        for(int i=1; i<=n; i++){
            int ht = Integer.parseInt(st1.nextToken());
            max = Math.max(max, ht);
            if(ht < max){
                sum += (max-ht);
            }
        }

        out.println(sum);
        out.close();
    }
} public class Main {
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);

        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        StringTokenizer st1 = new StringTokenizer(br.readLine());

        long sum = 0;
        int max = Integer.MIN_VALUE;

        for(int i=1; i<=n; i++){
            int ht = Integer.parseInt(st1.nextToken());
            max = Math.max(max, ht);
            if(ht < max){
                sum += (max-ht);
            }
        }

        out.println(sum);
        out.close();
    }
} Main
// package Contest#176;
import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);

        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        StringTokenizer st1 = new StringTokenizer(br.readLine());

        long sum = 0;
        int max = Integer.MIN_VALUE;

        for(int i=1; i<=n; i++){
            int ht = Integer.parseInt(st1.nextToken());
            max = Math.max(max, ht);
            if(ht < max){
                sum += (max-ht);
            }
        }

        out.println(sum);
        out.close();
    }
}