import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int p = sc.nextInt();
		int before = 0;
		long dai = 0;
		for (int i = 0; i < p; i++) {
			int height = sc.nextInt();
			if(before > height) {
				dai+=before - height;
			}else {
				before = height;
			}
		}
		System.out.println(dai);
	}
} import java.util.*; util.* java public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int p = sc.nextInt();
		int before = 0;
		long dai = 0;
		for (int i = 0; i < p; i++) {
			int height = sc.nextInt();
			if(before > height) {
				dai+=before - height;
			}else {
				before = height;
			}
		}
		System.out.println(dai);
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int p = sc.nextInt();
		int before = 0;
		long dai = 0;
		for (int i = 0; i < p; i++) {
			int height = sc.nextInt();
			if(before > height) {
				dai+=before - height;
			}else {
				before = height;
			}
		}
		System.out.println(dai);
	} main {
		Scanner sc = new Scanner(System.in);
		int p = sc.nextInt();
		int before = 0;
		long dai = 0;
		for (int i = 0; i < p; i++) {
			int height = sc.nextInt();
			if(before > height) {
				dai+=before - height;
			}else {
				before = height;
			}
		}
		System.out.println(dai);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int p = sc.nextInt(); p sc.nextInt() sc.nextInt sc int before = 0; before 0 long dai = 0; dai 0 for (int i = 0; i < p; i++) {
			int height = sc.nextInt();
			if(before > height) {
				dai+=before - height;
			}else {
				before = height;
			}
		} int i = 0; int i = 0; i 0 i < p i p i++ i++ i {
			int height = sc.nextInt();
			if(before > height) {
				dai+=before - height;
			}else {
				before = height;
			}
		} {
			int height = sc.nextInt();
			if(before > height) {
				dai+=before - height;
			}else {
				before = height;
			}
		} int height = sc.nextInt(); height sc.nextInt() sc.nextInt sc if(before > height) {
				dai+=before - height;
			}else {
				before = height;
			} before > height before height {
				dai+=before - height;
			} dai+=before - height dai before - height before height {
				before = height;
			} before = height before height System.out.println(dai) System.out.println System.out System System.out dai String[] args args public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int p = sc.nextInt();
		int before = 0;
		long dai = 0;
		for (int i = 0; i < p; i++) {
			int height = sc.nextInt();
			if(before > height) {
				dai+=before - height;
			}else {
				before = height;
			}
		}
		System.out.println(dai);
	}
} public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int p = sc.nextInt();
		int before = 0;
		long dai = 0;
		for (int i = 0; i < p; i++) {
			int height = sc.nextInt();
			if(before > height) {
				dai+=before - height;
			}else {
				before = height;
			}
		}
		System.out.println(dai);
	}
} Main
import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int p = sc.nextInt();
		int before = 0;
		long dai = 0;
		for (int i = 0; i < p; i++) {
			int height = sc.nextInt();
			if(before > height) {
				dai+=before - height;
			}else {
				before = height;
			}
		}
		System.out.println(dai);
	}
}