import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Object o = run();
        if (o != null)
            System.out.println(o);
    }

    static Object run() {
        try (Scanner sc = new Scanner(System.in)) {
            int n = sc.nextInt();
            ArrayList<Integer> a = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                a.add(sc.nextInt());
            }
            long latest = 0;
            long result = 0;
            for (int i = 0; i < n; i++) {
                if (latest > a.get(i)) {
                    result += latest - a.get(i);
                } else {
                    latest = a.get(i);
                }
            }
            return result;
        }
    }
}

class ChenNumberUtil {
    static Collection<Integer> getDivisors(int n) {
        ArrayList<Integer> al = new ArrayList<>();
        for (int i = 1; i * i <= n; i++) {
            if (n % i == 0) {
                al.add(i);
                if (n / i != i)
                    al.add(n / i);
            }
        }
        Collections.sort(al);
        return al;
    }
}

class ChenBitUtil {
    static int popCount(int bit) {
        return Integer.bitCount(bit);
    }

    static boolean isFlag(int bit, int i) {
        return 0 != (bit & (1 << i));
    }

    static int setTrue(int bit, int i) {
        return bit |= (1 << i);
    }

    static int setFalse(int bit, int i) {
        return bit &= ~(1 << i);
    }

    static int setReverse(int bit, int i) {
        return isFlag(bit, i) ? setFalse(bit, i) : setTrue(bit, i);
    }

    static int reverseAll(int bit) {
        return ~bit;
    }
} import java.util.ArrayList; ArrayList java.util import java.util.Collection; Collection java.util import java.util.Collections; Collections java.util import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Object o = run();
        if (o != null)
            System.out.println(o);
    }

    static Object run() {
        try (Scanner sc = new Scanner(System.in)) {
            int n = sc.nextInt();
            ArrayList<Integer> a = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                a.add(sc.nextInt());
            }
            long latest = 0;
            long result = 0;
            for (int i = 0; i < n; i++) {
                if (latest > a.get(i)) {
                    result += latest - a.get(i);
                } else {
                    latest = a.get(i);
                }
            }
            return result;
        }
    }
} Main public static void main(String[] args) {
        Object o = run();
        if (o != null)
            System.out.println(o);
    } main {
        Object o = run();
        if (o != null)
            System.out.println(o);
    } Object o = run(); o run() run if (o != null)
            System.out.println(o); o != null o null System.out.println(o) System.out.println System.out System System.out o String[] args args static Object run() {
        try (Scanner sc = new Scanner(System.in)) {
            int n = sc.nextInt();
            ArrayList<Integer> a = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                a.add(sc.nextInt());
            }
            long latest = 0;
            long result = 0;
            for (int i = 0; i < n; i++) {
                if (latest > a.get(i)) {
                    result += latest - a.get(i);
                } else {
                    latest = a.get(i);
                }
            }
            return result;
        }
    } run {
        try (Scanner sc = new Scanner(System.in)) {
            int n = sc.nextInt();
            ArrayList<Integer> a = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                a.add(sc.nextInt());
            }
            long latest = 0;
            long result = 0;
            for (int i = 0; i < n; i++) {
                if (latest > a.get(i)) {
                    result += latest - a.get(i);
                } else {
                    latest = a.get(i);
                }
            }
            return result;
        }
    } try (Scanner sc = new Scanner(System.in)) {
            int n = sc.nextInt();
            ArrayList<Integer> a = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                a.add(sc.nextInt());
            }
            long latest = 0;
            long result = 0;
            for (int i = 0; i < n; i++) {
                if (latest > a.get(i)) {
                    result += latest - a.get(i);
                } else {
                    latest = a.get(i);
                }
            }
            return result;
        } {
            int n = sc.nextInt();
            ArrayList<Integer> a = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                a.add(sc.nextInt());
            }
            long latest = 0;
            long result = 0;
            for (int i = 0; i < n; i++) {
                if (latest > a.get(i)) {
                    result += latest - a.get(i);
                } else {
                    latest = a.get(i);
                }
            }
            return result;
        } Scanner sc = new Scanner(System.in) Scanner sc = new Scanner(System.in) new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc ArrayList<Integer> a = new ArrayList<>(); a new ArrayList<>() for (int i = 0; i < n; i++) {
                a.add(sc.nextInt());
            } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
                a.add(sc.nextInt());
            } {
                a.add(sc.nextInt());
            } a.add(sc.nextInt()) a.add a sc.nextInt() sc.nextInt sc long latest = 0; latest 0 long result = 0; result 0 for (int i = 0; i < n; i++) {
                if (latest > a.get(i)) {
                    result += latest - a.get(i);
                } else {
                    latest = a.get(i);
                }
            } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
                if (latest > a.get(i)) {
                    result += latest - a.get(i);
                } else {
                    latest = a.get(i);
                }
            } {
                if (latest > a.get(i)) {
                    result += latest - a.get(i);
                } else {
                    latest = a.get(i);
                }
            } if (latest > a.get(i)) {
                    result += latest - a.get(i);
                } else {
                    latest = a.get(i);
                } latest > a.get(i) latest a.get(i) a.get a i {
                    result += latest - a.get(i);
                } result += latest - a.get(i) result latest - a.get(i) latest a.get(i) a.get a i {
                    latest = a.get(i);
                } latest = a.get(i) latest a.get(i) a.get a i return result; result class ChenNumberUtil {
    static Collection<Integer> getDivisors(int n) {
        ArrayList<Integer> al = new ArrayList<>();
        for (int i = 1; i * i <= n; i++) {
            if (n % i == 0) {
                al.add(i);
                if (n / i != i)
                    al.add(n / i);
            }
        }
        Collections.sort(al);
        return al;
    }
} ChenNumberUtil static Collection<Integer> getDivisors(int n) {
        ArrayList<Integer> al = new ArrayList<>();
        for (int i = 1; i * i <= n; i++) {
            if (n % i == 0) {
                al.add(i);
                if (n / i != i)
                    al.add(n / i);
            }
        }
        Collections.sort(al);
        return al;
    } getDivisors {
        ArrayList<Integer> al = new ArrayList<>();
        for (int i = 1; i * i <= n; i++) {
            if (n % i == 0) {
                al.add(i);
                if (n / i != i)
                    al.add(n / i);
            }
        }
        Collections.sort(al);
        return al;
    } ArrayList<Integer> al = new ArrayList<>(); al new ArrayList<>() for (int i = 1; i * i <= n; i++) {
            if (n % i == 0) {
                al.add(i);
                if (n / i != i)
                    al.add(n / i);
            }
        } int i = 1; int i = 1; i 1 i * i <= n i * i i i n i++ i++ i {
            if (n % i == 0) {
                al.add(i);
                if (n / i != i)
                    al.add(n / i);
            }
        } {
            if (n % i == 0) {
                al.add(i);
                if (n / i != i)
                    al.add(n / i);
            }
        } if (n % i == 0) {
                al.add(i);
                if (n / i != i)
                    al.add(n / i);
            } n % i == 0 n % i n i 0 {
                al.add(i);
                if (n / i != i)
                    al.add(n / i);
            } al.add(i) al.add al i if (n / i != i)
                    al.add(n / i); n / i != i n / i n i i al.add(n / i) al.add al n / i n i Collections.sort(al) Collections.sort Collections al return al; al int n n class ChenBitUtil {
    static int popCount(int bit) {
        return Integer.bitCount(bit);
    }

    static boolean isFlag(int bit, int i) {
        return 0 != (bit & (1 << i));
    }

    static int setTrue(int bit, int i) {
        return bit |= (1 << i);
    }

    static int setFalse(int bit, int i) {
        return bit &= ~(1 << i);
    }

    static int setReverse(int bit, int i) {
        return isFlag(bit, i) ? setFalse(bit, i) : setTrue(bit, i);
    }

    static int reverseAll(int bit) {
        return ~bit;
    }
} ChenBitUtil static int popCount(int bit) {
        return Integer.bitCount(bit);
    } popCount {
        return Integer.bitCount(bit);
    } return Integer.bitCount(bit); Integer.bitCount(bit) Integer.bitCount Integer bit int bit bit static boolean isFlag(int bit, int i) {
        return 0 != (bit & (1 << i));
    } isFlag {
        return 0 != (bit & (1 << i));
    } return 0 != (bit & (1 << i)); 0 != (bit & (1 << i)) 0 (bit & (1 << i)) bit (1 << i) 1 i int bit bit int i i static int setTrue(int bit, int i) {
        return bit |= (1 << i);
    } setTrue {
        return bit |= (1 << i);
    } return bit |= (1 << i); bit |= (1 << i) bit (1 << i) 1 i int bit bit int i i static int setFalse(int bit, int i) {
        return bit &= ~(1 << i);
    } setFalse {
        return bit &= ~(1 << i);
    } return bit &= ~(1 << i); bit &= ~(1 << i) bit ~(1 << i) (1 << i) 1 i int bit bit int i i static int setReverse(int bit, int i) {
        return isFlag(bit, i) ? setFalse(bit, i) : setTrue(bit, i);
    } setReverse {
        return isFlag(bit, i) ? setFalse(bit, i) : setTrue(bit, i);
    } return isFlag(bit, i) ? setFalse(bit, i) : setTrue(bit, i); isFlag(bit, i) ? setFalse(bit, i) : setTrue(bit, i) isFlag(bit, i) isFlag bit i setFalse(bit, i) setFalse bit i setTrue(bit, i) setTrue bit i int bit bit int i i static int reverseAll(int bit) {
        return ~bit;
    } reverseAll {
        return ~bit;
    } return ~bit; ~bit bit int bit bit public class Main {
    public static void main(String[] args) {
        Object o = run();
        if (o != null)
            System.out.println(o);
    }

    static Object run() {
        try (Scanner sc = new Scanner(System.in)) {
            int n = sc.nextInt();
            ArrayList<Integer> a = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                a.add(sc.nextInt());
            }
            long latest = 0;
            long result = 0;
            for (int i = 0; i < n; i++) {
                if (latest > a.get(i)) {
                    result += latest - a.get(i);
                } else {
                    latest = a.get(i);
                }
            }
            return result;
        }
    }
} public class Main {
    public static void main(String[] args) {
        Object o = run();
        if (o != null)
            System.out.println(o);
    }

    static Object run() {
        try (Scanner sc = new Scanner(System.in)) {
            int n = sc.nextInt();
            ArrayList<Integer> a = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                a.add(sc.nextInt());
            }
            long latest = 0;
            long result = 0;
            for (int i = 0; i < n; i++) {
                if (latest > a.get(i)) {
                    result += latest - a.get(i);
                } else {
                    latest = a.get(i);
                }
            }
            return result;
        }
    }
} Main
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Object o = run();
        if (o != null)
            System.out.println(o);
    }

    static Object run() {
        try (Scanner sc = new Scanner(System.in)) {
            int n = sc.nextInt();
            ArrayList<Integer> a = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                a.add(sc.nextInt());
            }
            long latest = 0;
            long result = 0;
            for (int i = 0; i < n; i++) {
                if (latest > a.get(i)) {
                    result += latest - a.get(i);
                } else {
                    latest = a.get(i);
                }
            }
            return result;
        }
    }
}

class ChenNumberUtil {
    static Collection<Integer> getDivisors(int n) {
        ArrayList<Integer> al = new ArrayList<>();
        for (int i = 1; i * i <= n; i++) {
            if (n % i == 0) {
                al.add(i);
                if (n / i != i)
                    al.add(n / i);
            }
        }
        Collections.sort(al);
        return al;
    }
}

class ChenBitUtil {
    static int popCount(int bit) {
        return Integer.bitCount(bit);
    }

    static boolean isFlag(int bit, int i) {
        return 0 != (bit & (1 << i));
    }

    static int setTrue(int bit, int i) {
        return bit |= (1 << i);
    }

    static int setFalse(int bit, int i) {
        return bit &= ~(1 << i);
    }

    static int setReverse(int bit, int i) {
        return isFlag(bit, i) ? setFalse(bit, i) : setTrue(bit, i);
    }

    static int reverseAll(int bit) {
        return ~bit;
    }
}
