import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {
    void solve(Scanner sc, PrintWriter pw) {
        int N = sc.nextInt();
        long total = 0;
        int currentMax = Integer.MIN_VALUE;
        for (int i = 0; i < N; i++) {
            int val = sc.nextInt();
            int nextMax = Math.max(currentMax, val);
            if (i > 0) {
                total += nextMax - val;
            }
            currentMax = nextMax;
        }
        pw.println(total);
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        new Main().solve(sc, pw);
        pw.flush();
        pw.close();
        sc.close();
    }
} import java.io.PrintWriter; PrintWriter java.io import java.math.BigInteger; BigInteger java.math import java.util.HashMap; HashMap java.util import java.util.HashSet; HashSet java.util import java.util.Scanner; Scanner java.util import java.util.Set; Set java.util public class Main {
    void solve(Scanner sc, PrintWriter pw) {
        int N = sc.nextInt();
        long total = 0;
        int currentMax = Integer.MIN_VALUE;
        for (int i = 0; i < N; i++) {
            int val = sc.nextInt();
            int nextMax = Math.max(currentMax, val);
            if (i > 0) {
                total += nextMax - val;
            }
            currentMax = nextMax;
        }
        pw.println(total);
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        new Main().solve(sc, pw);
        pw.flush();
        pw.close();
        sc.close();
    }
} Main void solve(Scanner sc, PrintWriter pw) {
        int N = sc.nextInt();
        long total = 0;
        int currentMax = Integer.MIN_VALUE;
        for (int i = 0; i < N; i++) {
            int val = sc.nextInt();
            int nextMax = Math.max(currentMax, val);
            if (i > 0) {
                total += nextMax - val;
            }
            currentMax = nextMax;
        }
        pw.println(total);
    } solve {
        int N = sc.nextInt();
        long total = 0;
        int currentMax = Integer.MIN_VALUE;
        for (int i = 0; i < N; i++) {
            int val = sc.nextInt();
            int nextMax = Math.max(currentMax, val);
            if (i > 0) {
                total += nextMax - val;
            }
            currentMax = nextMax;
        }
        pw.println(total);
    } int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc long total = 0; total 0 int currentMax = Integer.MIN_VALUE; currentMax Integer.MIN_VALUE Integer Integer.MIN_VALUE for (int i = 0; i < N; i++) {
            int val = sc.nextInt();
            int nextMax = Math.max(currentMax, val);
            if (i > 0) {
                total += nextMax - val;
            }
            currentMax = nextMax;
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            int val = sc.nextInt();
            int nextMax = Math.max(currentMax, val);
            if (i > 0) {
                total += nextMax - val;
            }
            currentMax = nextMax;
        } {
            int val = sc.nextInt();
            int nextMax = Math.max(currentMax, val);
            if (i > 0) {
                total += nextMax - val;
            }
            currentMax = nextMax;
        } int val = sc.nextInt(); val sc.nextInt() sc.nextInt sc int nextMax = Math.max(currentMax, val); nextMax Math.max(currentMax, val) Math.max Math currentMax val if (i > 0) {
                total += nextMax - val;
            } i > 0 i 0 {
                total += nextMax - val;
            } total += nextMax - val total nextMax - val nextMax val currentMax = nextMax currentMax nextMax pw.println(total) pw.println pw total Scanner sc sc PrintWriter pw pw public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        new Main().solve(sc, pw);
        pw.flush();
        pw.close();
        sc.close();
    } main {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        new Main().solve(sc, pw);
        pw.flush();
        pw.close();
        sc.close();
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) PrintWriter pw = new PrintWriter(System.out); pw new PrintWriter(System.out) new Main().solve(sc, pw) new Main().solve new Main() sc pw pw.flush() pw.flush pw pw.close() pw.close pw sc.close() sc.close sc String[] args args public class Main {
    void solve(Scanner sc, PrintWriter pw) {
        int N = sc.nextInt();
        long total = 0;
        int currentMax = Integer.MIN_VALUE;
        for (int i = 0; i < N; i++) {
            int val = sc.nextInt();
            int nextMax = Math.max(currentMax, val);
            if (i > 0) {
                total += nextMax - val;
            }
            currentMax = nextMax;
        }
        pw.println(total);
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        new Main().solve(sc, pw);
        pw.flush();
        pw.close();
        sc.close();
    }
} public class Main {
    void solve(Scanner sc, PrintWriter pw) {
        int N = sc.nextInt();
        long total = 0;
        int currentMax = Integer.MIN_VALUE;
        for (int i = 0; i < N; i++) {
            int val = sc.nextInt();
            int nextMax = Math.max(currentMax, val);
            if (i > 0) {
                total += nextMax - val;
            }
            currentMax = nextMax;
        }
        pw.println(total);
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        new Main().solve(sc, pw);
        pw.flush();
        pw.close();
        sc.close();
    }
} Main
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {
    void solve(Scanner sc, PrintWriter pw) {
        int N = sc.nextInt();
        long total = 0;
        int currentMax = Integer.MIN_VALUE;
        for (int i = 0; i < N; i++) {
            int val = sc.nextInt();
            int nextMax = Math.max(currentMax, val);
            if (i > 0) {
                total += nextMax - val;
            }
            currentMax = nextMax;
        }
        pw.println(total);
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        new Main().solve(sc, pw);
        pw.flush();
        pw.close();
        sc.close();
    }
}
