import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new java.io.InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        StringTokenizer st = new StringTokenizer(br.readLine());

        int N = Integer.parseInt(st.nextToken());
        String[] tall_array = br.readLine().split(" ");
        int tmp = 0;
        long result = 0;

        for(int i = 0; i < N; i++) {

            int target = Integer.parseInt(tall_array[i]);

            if(target < tmp) {
                result += tmp - target;
            } else {
                tmp = target;
            }
        }

        pw.println(result);
        br.close();
        pw.close();
    }
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.PrintWriter; PrintWriter java.io import java.util.StringTokenizer; StringTokenizer java.util public class Main {
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new java.io.InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        StringTokenizer st = new StringTokenizer(br.readLine());

        int N = Integer.parseInt(st.nextToken());
        String[] tall_array = br.readLine().split(" ");
        int tmp = 0;
        long result = 0;

        for(int i = 0; i < N; i++) {

            int target = Integer.parseInt(tall_array[i]);

            if(target < tmp) {
                result += tmp - target;
            } else {
                tmp = target;
            }
        }

        pw.println(result);
        br.close();
        pw.close();
    }
} Main public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new java.io.InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        StringTokenizer st = new StringTokenizer(br.readLine());

        int N = Integer.parseInt(st.nextToken());
        String[] tall_array = br.readLine().split(" ");
        int tmp = 0;
        long result = 0;

        for(int i = 0; i < N; i++) {

            int target = Integer.parseInt(tall_array[i]);

            if(target < tmp) {
                result += tmp - target;
            } else {
                tmp = target;
            }
        }

        pw.println(result);
        br.close();
        pw.close();
    } main {
        BufferedReader br = new BufferedReader(new java.io.InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        StringTokenizer st = new StringTokenizer(br.readLine());

        int N = Integer.parseInt(st.nextToken());
        String[] tall_array = br.readLine().split(" ");
        int tmp = 0;
        long result = 0;

        for(int i = 0; i < N; i++) {

            int target = Integer.parseInt(tall_array[i]);

            if(target < tmp) {
                result += tmp - target;
            } else {
                tmp = target;
            }
        }

        pw.println(result);
        br.close();
        pw.close();
    } BufferedReader br = new BufferedReader(new java.io.InputStreamReader(System.in)); br new BufferedReader(new java.io.InputStreamReader(System.in)) PrintWriter pw = new PrintWriter(System.out); pw 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 String[] tall_array = br.readLine().split(" "); tall_array br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int tmp = 0; tmp 0 long result = 0; result 0 for(int i = 0; i < N; i++) {

            int target = Integer.parseInt(tall_array[i]);

            if(target < tmp) {
                result += tmp - target;
            } else {
                tmp = target;
            }
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {

            int target = Integer.parseInt(tall_array[i]);

            if(target < tmp) {
                result += tmp - target;
            } else {
                tmp = target;
            }
        } {

            int target = Integer.parseInt(tall_array[i]);

            if(target < tmp) {
                result += tmp - target;
            } else {
                tmp = target;
            }
        } int target = Integer.parseInt(tall_array[i]); target Integer.parseInt(tall_array[i]) Integer.parseInt Integer tall_array[i] tall_array i if(target < tmp) {
                result += tmp - target;
            } else {
                tmp = target;
            } target < tmp target tmp {
                result += tmp - target;
            } result += tmp - target result tmp - target tmp target {
                tmp = target;
            } tmp = target tmp target pw.println(result) pw.println pw result br.close() br.close br pw.close() pw.close pw String[] args args public class Main {
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new java.io.InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        StringTokenizer st = new StringTokenizer(br.readLine());

        int N = Integer.parseInt(st.nextToken());
        String[] tall_array = br.readLine().split(" ");
        int tmp = 0;
        long result = 0;

        for(int i = 0; i < N; i++) {

            int target = Integer.parseInt(tall_array[i]);

            if(target < tmp) {
                result += tmp - target;
            } else {
                tmp = target;
            }
        }

        pw.println(result);
        br.close();
        pw.close();
    }
} public class Main {
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new java.io.InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        StringTokenizer st = new StringTokenizer(br.readLine());

        int N = Integer.parseInt(st.nextToken());
        String[] tall_array = br.readLine().split(" ");
        int tmp = 0;
        long result = 0;

        for(int i = 0; i < N; i++) {

            int target = Integer.parseInt(tall_array[i]);

            if(target < tmp) {
                result += tmp - target;
            } else {
                tmp = target;
            }
        }

        pw.println(result);
        br.close();
        pw.close();
    }
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new java.io.InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        StringTokenizer st = new StringTokenizer(br.readLine());

        int N = Integer.parseInt(st.nextToken());
        String[] tall_array = br.readLine().split(" ");
        int tmp = 0;
        long result = 0;

        for(int i = 0; i < N; i++) {

            int target = Integer.parseInt(tall_array[i]);

            if(target < tmp) {
                result += tmp - target;
            } else {
                tmp = target;
            }
        }

        pw.println(result);
        br.close();
        pw.close();
    }
}