import java.util.*;

public class Main {
    public static void main(final String[] args) {
        final Scanner sc = new Scanner(System.in);
        // 整数の入力
        // final int a = sc.nextInt();
        // 文字列の入力
        // final String s1 = sc.next();
        final int varN = Integer.parseInt(sc.next());
        int lastA = Integer.parseInt(sc.next());
        int presentA = 0;
        long cnt = 0;
        for (int i = 1; i < varN; i++) {
            presentA = Integer.parseInt(sc.next());
            if (presentA < lastA) {
                cnt = cnt + lastA - presentA;
                continue;
            } else {
                lastA = presentA;
            }
        }
        sc.close();

        System.out.println(cnt);
    }
} import java.util.*; util.* java public class Main {
    public static void main(final String[] args) {
        final Scanner sc = new Scanner(System.in);
        // 整数の入力
        // final int a = sc.nextInt();
        // 文字列の入力
        // final String s1 = sc.next();
        final int varN = Integer.parseInt(sc.next());
        int lastA = Integer.parseInt(sc.next());
        int presentA = 0;
        long cnt = 0;
        for (int i = 1; i < varN; i++) {
            presentA = Integer.parseInt(sc.next());
            if (presentA < lastA) {
                cnt = cnt + lastA - presentA;
                continue;
            } else {
                lastA = presentA;
            }
        }
        sc.close();

        System.out.println(cnt);
    }
} Main public static void main(final String[] args) {
        final Scanner sc = new Scanner(System.in);
        // 整数の入力
        // final int a = sc.nextInt();
        // 文字列の入力
        // final String s1 = sc.next();
        final int varN = Integer.parseInt(sc.next());
        int lastA = Integer.parseInt(sc.next());
        int presentA = 0;
        long cnt = 0;
        for (int i = 1; i < varN; i++) {
            presentA = Integer.parseInt(sc.next());
            if (presentA < lastA) {
                cnt = cnt + lastA - presentA;
                continue;
            } else {
                lastA = presentA;
            }
        }
        sc.close();

        System.out.println(cnt);
    } main {
        final Scanner sc = new Scanner(System.in);
        // 整数の入力
        // final int a = sc.nextInt();
        // 文字列の入力
        // final String s1 = sc.next();
        final int varN = Integer.parseInt(sc.next());
        int lastA = Integer.parseInt(sc.next());
        int presentA = 0;
        long cnt = 0;
        for (int i = 1; i < varN; i++) {
            presentA = Integer.parseInt(sc.next());
            if (presentA < lastA) {
                cnt = cnt + lastA - presentA;
                continue;
            } else {
                lastA = presentA;
            }
        }
        sc.close();

        System.out.println(cnt);
    } final Scanner sc = new Scanner(System.in); sc new Scanner(System.in) final int varN = Integer.parseInt(sc.next()); varN Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int lastA = Integer.parseInt(sc.next()); lastA Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int presentA = 0; presentA 0 long cnt = 0; cnt 0 for (int i = 1; i < varN; i++) {
            presentA = Integer.parseInt(sc.next());
            if (presentA < lastA) {
                cnt = cnt + lastA - presentA;
                continue;
            } else {
                lastA = presentA;
            }
        } int i = 1; int i = 1; i 1 i < varN i varN i++ i++ i {
            presentA = Integer.parseInt(sc.next());
            if (presentA < lastA) {
                cnt = cnt + lastA - presentA;
                continue;
            } else {
                lastA = presentA;
            }
        } {
            presentA = Integer.parseInt(sc.next());
            if (presentA < lastA) {
                cnt = cnt + lastA - presentA;
                continue;
            } else {
                lastA = presentA;
            }
        } presentA = Integer.parseInt(sc.next()) presentA Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc if (presentA < lastA) {
                cnt = cnt + lastA - presentA;
                continue;
            } else {
                lastA = presentA;
            } presentA < lastA presentA lastA {
                cnt = cnt + lastA - presentA;
                continue;
            } cnt = cnt + lastA - presentA cnt cnt + lastA - presentA cnt + lastA cnt lastA presentA continue; {
                lastA = presentA;
            } lastA = presentA lastA presentA sc.close() sc.close sc System.out.println(cnt) System.out.println System.out System System.out cnt final String[] args args public class Main {
    public static void main(final String[] args) {
        final Scanner sc = new Scanner(System.in);
        // 整数の入力
        // final int a = sc.nextInt();
        // 文字列の入力
        // final String s1 = sc.next();
        final int varN = Integer.parseInt(sc.next());
        int lastA = Integer.parseInt(sc.next());
        int presentA = 0;
        long cnt = 0;
        for (int i = 1; i < varN; i++) {
            presentA = Integer.parseInt(sc.next());
            if (presentA < lastA) {
                cnt = cnt + lastA - presentA;
                continue;
            } else {
                lastA = presentA;
            }
        }
        sc.close();

        System.out.println(cnt);
    }
} public class Main {
    public static void main(final String[] args) {
        final Scanner sc = new Scanner(System.in);
        // 整数の入力
        // final int a = sc.nextInt();
        // 文字列の入力
        // final String s1 = sc.next();
        final int varN = Integer.parseInt(sc.next());
        int lastA = Integer.parseInt(sc.next());
        int presentA = 0;
        long cnt = 0;
        for (int i = 1; i < varN; i++) {
            presentA = Integer.parseInt(sc.next());
            if (presentA < lastA) {
                cnt = cnt + lastA - presentA;
                continue;
            } else {
                lastA = presentA;
            }
        }
        sc.close();

        System.out.println(cnt);
    }
} Main
import java.util.*;

public class Main {
    public static void main(final String[] args) {
        final Scanner sc = new Scanner(System.in);
        // 整数の入力
        // final int a = sc.nextInt();
        // 文字列の入力
        // final String s1 = sc.next();
        final int varN = Integer.parseInt(sc.next());
        int lastA = Integer.parseInt(sc.next());
        int presentA = 0;
        long cnt = 0;
        for (int i = 1; i < varN; i++) {
            presentA = Integer.parseInt(sc.next());
            if (presentA < lastA) {
                cnt = cnt + lastA - presentA;
                continue;
            } else {
                lastA = presentA;
            }
        }
        sc.close();

        System.out.println(cnt);
    }
}