import java.util.Scanner;

public class Main {
        public static void main(String[] args) {
                Scanner scan = new Scanner(System.in);

                int N = scan.nextInt();

                long sum = 0;

                long mae = 0;
                long now = 0;

                for (int i = 0; i < N; i++) {
                	now = scan.nextLong();
                	if (now < mae) {
                		sum += mae - now;
                		now = mae;
                	}
                	mae = now;
                }

                System.out.println(sum);

                scan.close();

        }

} import java.util.Scanner; Scanner java.util public class Main {
        public static void main(String[] args) {
                Scanner scan = new Scanner(System.in);

                int N = scan.nextInt();

                long sum = 0;

                long mae = 0;
                long now = 0;

                for (int i = 0; i < N; i++) {
                	now = scan.nextLong();
                	if (now < mae) {
                		sum += mae - now;
                		now = mae;
                	}
                	mae = now;
                }

                System.out.println(sum);

                scan.close();

        }

} Main public static void main(String[] args) {
                Scanner scan = new Scanner(System.in);

                int N = scan.nextInt();

                long sum = 0;

                long mae = 0;
                long now = 0;

                for (int i = 0; i < N; i++) {
                	now = scan.nextLong();
                	if (now < mae) {
                		sum += mae - now;
                		now = mae;
                	}
                	mae = now;
                }

                System.out.println(sum);

                scan.close();

        } main {
                Scanner scan = new Scanner(System.in);

                int N = scan.nextInt();

                long sum = 0;

                long mae = 0;
                long now = 0;

                for (int i = 0; i < N; i++) {
                	now = scan.nextLong();
                	if (now < mae) {
                		sum += mae - now;
                		now = mae;
                	}
                	mae = now;
                }

                System.out.println(sum);

                scan.close();

        } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int N = scan.nextInt(); N scan.nextInt() scan.nextInt scan long sum = 0; sum 0 long mae = 0; mae 0 long now = 0; now 0 for (int i = 0; i < N; i++) {
                	now = scan.nextLong();
                	if (now < mae) {
                		sum += mae - now;
                		now = mae;
                	}
                	mae = now;
                } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
                	now = scan.nextLong();
                	if (now < mae) {
                		sum += mae - now;
                		now = mae;
                	}
                	mae = now;
                } {
                	now = scan.nextLong();
                	if (now < mae) {
                		sum += mae - now;
                		now = mae;
                	}
                	mae = now;
                } now = scan.nextLong() now scan.nextLong() scan.nextLong scan if (now < mae) {
                		sum += mae - now;
                		now = mae;
                	} now < mae now mae {
                		sum += mae - now;
                		now = mae;
                	} sum += mae - now sum mae - now mae now now = mae now mae mae = now mae now System.out.println(sum) System.out.println System.out System System.out sum scan.close() scan.close scan String[] args args public class Main {
        public static void main(String[] args) {
                Scanner scan = new Scanner(System.in);

                int N = scan.nextInt();

                long sum = 0;

                long mae = 0;
                long now = 0;

                for (int i = 0; i < N; i++) {
                	now = scan.nextLong();
                	if (now < mae) {
                		sum += mae - now;
                		now = mae;
                	}
                	mae = now;
                }

                System.out.println(sum);

                scan.close();

        }

} public class Main {
        public static void main(String[] args) {
                Scanner scan = new Scanner(System.in);

                int N = scan.nextInt();

                long sum = 0;

                long mae = 0;
                long now = 0;

                for (int i = 0; i < N; i++) {
                	now = scan.nextLong();
                	if (now < mae) {
                		sum += mae - now;
                		now = mae;
                	}
                	mae = now;
                }

                System.out.println(sum);

                scan.close();

        }

} Main
import java.util.Scanner;

public class Main {
        public static void main(String[] args) {
                Scanner scan = new Scanner(System.in);

                int N = scan.nextInt();

                long sum = 0;

                long mae = 0;
                long now = 0;

                for (int i = 0; i < N; i++) {
                	now = scan.nextLong();
                	if (now < mae) {
                		sum += mae - now;
                		now = mae;
                	}
                	mae = now;
                }

                System.out.println(sum);

                scan.close();

        }

}