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

/**
 * Created by Ayushi on 22 Aug 2020.
 * Problem:
 * Round:
 */

public class Main {
    public static void main(String[] args) throws IOException {
        StringTokenizer st;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        st = new StringTokenizer(br.readLine(), " ");
        br.close();
        int p = 0;
        int a;
        long ans = 0;
        while (st.hasMoreTokens()) {
            a = Integer.parseInt(st.nextToken());
            if (a < p) {
                ans += p-a;
            }
            else p = a;
        }
        System.out.println(ans);
    }
} import java.io.*; io.* java import java.util.*; util.* java public class Main {
    public static void main(String[] args) throws IOException {
        StringTokenizer st;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        st = new StringTokenizer(br.readLine(), " ");
        br.close();
        int p = 0;
        int a;
        long ans = 0;
        while (st.hasMoreTokens()) {
            a = Integer.parseInt(st.nextToken());
            if (a < p) {
                ans += p-a;
            }
            else p = a;
        }
        System.out.println(ans);
    }
} Main public static void main(String[] args) throws IOException {
        StringTokenizer st;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        st = new StringTokenizer(br.readLine(), " ");
        br.close();
        int p = 0;
        int a;
        long ans = 0;
        while (st.hasMoreTokens()) {
            a = Integer.parseInt(st.nextToken());
            if (a < p) {
                ans += p-a;
            }
            else p = a;
        }
        System.out.println(ans);
    } main {
        StringTokenizer st;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        st = new StringTokenizer(br.readLine(), " ");
        br.close();
        int p = 0;
        int a;
        long ans = 0;
        while (st.hasMoreTokens()) {
            a = Integer.parseInt(st.nextToken());
            if (a < p) {
                ans += p-a;
            }
            else p = a;
        }
        System.out.println(ans);
    } StringTokenizer st; st BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) int n = Integer.parseInt(br.readLine()); n Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br st = new StringTokenizer(br.readLine(), " ") st new StringTokenizer(br.readLine(), " ") br.close() br.close br int p = 0; p 0 int a; a long ans = 0; ans 0 while (st.hasMoreTokens()) {
            a = Integer.parseInt(st.nextToken());
            if (a < p) {
                ans += p-a;
            }
            else p = a;
        } st.hasMoreTokens() st.hasMoreTokens st {
            a = Integer.parseInt(st.nextToken());
            if (a < p) {
                ans += p-a;
            }
            else p = a;
        } a = Integer.parseInt(st.nextToken()) a Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st if (a < p) {
                ans += p-a;
            }
            else p = a; a < p a p {
                ans += p-a;
            } ans += p-a ans p-a p a p = a p a System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {
    public static void main(String[] args) throws IOException {
        StringTokenizer st;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        st = new StringTokenizer(br.readLine(), " ");
        br.close();
        int p = 0;
        int a;
        long ans = 0;
        while (st.hasMoreTokens()) {
            a = Integer.parseInt(st.nextToken());
            if (a < p) {
                ans += p-a;
            }
            else p = a;
        }
        System.out.println(ans);
    }
} public class Main {
    public static void main(String[] args) throws IOException {
        StringTokenizer st;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        st = new StringTokenizer(br.readLine(), " ");
        br.close();
        int p = 0;
        int a;
        long ans = 0;
        while (st.hasMoreTokens()) {
            a = Integer.parseInt(st.nextToken());
            if (a < p) {
                ans += p-a;
            }
            else p = a;
        }
        System.out.println(ans);
    }
} Main
import java.io.*;
import java.util.*;

/**
 * Created by Ayushi on 22 Aug 2020.
 * Problem:
 * Round:
 */

public class Main {
    public static void main(String[] args) throws IOException {
        StringTokenizer st;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        st = new StringTokenizer(br.readLine(), " ");
        br.close();
        int p = 0;
        int a;
        long ans = 0;
        while (st.hasMoreTokens()) {
            a = Integer.parseInt(st.nextToken());
            if (a < p) {
                ans += p-a;
            }
            else p = a;
        }
        System.out.println(ans);
    }
}
