import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long count = 0;
        int base = 0;
        int now = sc.nextInt();
        for (int i = 0; i < N-1; i++) {
            int Ai = sc.nextInt();
            base = Ai;
            if (Ai < now) {
                base = now;
                count += (now-Ai);
            }
            now = base;
        }
        System.out.println(count);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long count = 0;
        int base = 0;
        int now = sc.nextInt();
        for (int i = 0; i < N-1; i++) {
            int Ai = sc.nextInt();
            base = Ai;
            if (Ai < now) {
                base = now;
                count += (now-Ai);
            }
            now = base;
        }
        System.out.println(count);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long count = 0;
        int base = 0;
        int now = sc.nextInt();
        for (int i = 0; i < N-1; i++) {
            int Ai = sc.nextInt();
            base = Ai;
            if (Ai < now) {
                base = now;
                count += (now-Ai);
            }
            now = base;
        }
        System.out.println(count);
    } main {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long count = 0;
        int base = 0;
        int now = sc.nextInt();
        for (int i = 0; i < N-1; i++) {
            int Ai = sc.nextInt();
            base = Ai;
            if (Ai < now) {
                base = now;
                count += (now-Ai);
            }
            now = base;
        }
        System.out.println(count);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc long count = 0; count 0 int base = 0; base 0 int now = sc.nextInt(); now sc.nextInt() sc.nextInt sc for (int i = 0; i < N-1; i++) {
            int Ai = sc.nextInt();
            base = Ai;
            if (Ai < now) {
                base = now;
                count += (now-Ai);
            }
            now = base;
        } int i = 0; int i = 0; i 0 i < N-1 i N-1 N 1 i++ i++ i {
            int Ai = sc.nextInt();
            base = Ai;
            if (Ai < now) {
                base = now;
                count += (now-Ai);
            }
            now = base;
        } {
            int Ai = sc.nextInt();
            base = Ai;
            if (Ai < now) {
                base = now;
                count += (now-Ai);
            }
            now = base;
        } int Ai = sc.nextInt(); Ai sc.nextInt() sc.nextInt sc base = Ai base Ai if (Ai < now) {
                base = now;
                count += (now-Ai);
            } Ai < now Ai now {
                base = now;
                count += (now-Ai);
            } base = now base now count += (now-Ai) count (now-Ai) now Ai now = base now base System.out.println(count) System.out.println System.out System System.out count String[] args args public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long count = 0;
        int base = 0;
        int now = sc.nextInt();
        for (int i = 0; i < N-1; i++) {
            int Ai = sc.nextInt();
            base = Ai;
            if (Ai < now) {
                base = now;
                count += (now-Ai);
            }
            now = base;
        }
        System.out.println(count);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long count = 0;
        int base = 0;
        int now = sc.nextInt();
        for (int i = 0; i < N-1; i++) {
            int Ai = sc.nextInt();
            base = Ai;
            if (Ai < now) {
                base = now;
                count += (now-Ai);
            }
            now = base;
        }
        System.out.println(count);
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long count = 0;
        int base = 0;
        int now = sc.nextInt();
        for (int i = 0; i < N-1; i++) {
            int Ai = sc.nextInt();
            base = Ai;
            if (Ai < now) {
                base = now;
                count += (now-Ai);
            }
            now = base;
        }
        System.out.println(count);
    }
}