import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
		BigInteger bordersum = BigInteger.ZERO;
		int tmpborder = 0;
		for (int i = 0; i < n; i++) {
			int tmp = sc.nextInt();
			if (i == 0) {
				tmpborder = tmp;
				continue;
			}
			if (tmp < tmpborder) {
				bordersum = bordersum.add(BigInteger.valueOf(tmpborder).subtract(BigInteger.valueOf(tmp)));
			} else {
				tmpborder = tmp;
			}
		}
		System.out.println(bordersum);
	}
} import java.math.BigInteger; BigInteger java.math import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
		BigInteger bordersum = BigInteger.ZERO;
		int tmpborder = 0;
		for (int i = 0; i < n; i++) {
			int tmp = sc.nextInt();
			if (i == 0) {
				tmpborder = tmp;
				continue;
			}
			if (tmp < tmpborder) {
				bordersum = bordersum.add(BigInteger.valueOf(tmpborder).subtract(BigInteger.valueOf(tmp)));
			} else {
				tmpborder = tmp;
			}
		}
		System.out.println(bordersum);
	}
} Main public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
		BigInteger bordersum = BigInteger.ZERO;
		int tmpborder = 0;
		for (int i = 0; i < n; i++) {
			int tmp = sc.nextInt();
			if (i == 0) {
				tmpborder = tmp;
				continue;
			}
			if (tmp < tmpborder) {
				bordersum = bordersum.add(BigInteger.valueOf(tmpborder).subtract(BigInteger.valueOf(tmp)));
			} else {
				tmpborder = tmp;
			}
		}
		System.out.println(bordersum);
	} main {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
		BigInteger bordersum = BigInteger.ZERO;
		int tmpborder = 0;
		for (int i = 0; i < n; i++) {
			int tmp = sc.nextInt();
			if (i == 0) {
				tmpborder = tmp;
				continue;
			}
			if (tmp < tmpborder) {
				bordersum = bordersum.add(BigInteger.valueOf(tmpborder).subtract(BigInteger.valueOf(tmp)));
			} else {
				tmpborder = tmp;
			}
		}
		System.out.println(bordersum);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc sc.nextLine() sc.nextLine sc BigInteger bordersum = BigInteger.ZERO; bordersum BigInteger.ZERO BigInteger BigInteger.ZERO int tmpborder = 0; tmpborder 0 for (int i = 0; i < n; i++) {
			int tmp = sc.nextInt();
			if (i == 0) {
				tmpborder = tmp;
				continue;
			}
			if (tmp < tmpborder) {
				bordersum = bordersum.add(BigInteger.valueOf(tmpborder).subtract(BigInteger.valueOf(tmp)));
			} else {
				tmpborder = tmp;
			}
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			int tmp = sc.nextInt();
			if (i == 0) {
				tmpborder = tmp;
				continue;
			}
			if (tmp < tmpborder) {
				bordersum = bordersum.add(BigInteger.valueOf(tmpborder).subtract(BigInteger.valueOf(tmp)));
			} else {
				tmpborder = tmp;
			}
		} {
			int tmp = sc.nextInt();
			if (i == 0) {
				tmpborder = tmp;
				continue;
			}
			if (tmp < tmpborder) {
				bordersum = bordersum.add(BigInteger.valueOf(tmpborder).subtract(BigInteger.valueOf(tmp)));
			} else {
				tmpborder = tmp;
			}
		} int tmp = sc.nextInt(); tmp sc.nextInt() sc.nextInt sc if (i == 0) {
				tmpborder = tmp;
				continue;
			} i == 0 i 0 {
				tmpborder = tmp;
				continue;
			} tmpborder = tmp tmpborder tmp continue; if (tmp < tmpborder) {
				bordersum = bordersum.add(BigInteger.valueOf(tmpborder).subtract(BigInteger.valueOf(tmp)));
			} else {
				tmpborder = tmp;
			} tmp < tmpborder tmp tmpborder {
				bordersum = bordersum.add(BigInteger.valueOf(tmpborder).subtract(BigInteger.valueOf(tmp)));
			} bordersum = bordersum.add(BigInteger.valueOf(tmpborder).subtract(BigInteger.valueOf(tmp))) bordersum bordersum.add(BigInteger.valueOf(tmpborder).subtract(BigInteger.valueOf(tmp))) bordersum.add bordersum BigInteger.valueOf(tmpborder).subtract(BigInteger.valueOf(tmp)) BigInteger.valueOf(tmpborder).subtract BigInteger.valueOf(tmpborder) BigInteger.valueOf BigInteger tmpborder BigInteger.valueOf(tmp) BigInteger.valueOf BigInteger tmp {
				tmpborder = tmp;
			} tmpborder = tmp tmpborder tmp System.out.println(bordersum) System.out.println System.out System System.out bordersum String[] args args public class Main {
	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
		BigInteger bordersum = BigInteger.ZERO;
		int tmpborder = 0;
		for (int i = 0; i < n; i++) {
			int tmp = sc.nextInt();
			if (i == 0) {
				tmpborder = tmp;
				continue;
			}
			if (tmp < tmpborder) {
				bordersum = bordersum.add(BigInteger.valueOf(tmpborder).subtract(BigInteger.valueOf(tmp)));
			} else {
				tmpborder = tmp;
			}
		}
		System.out.println(bordersum);
	}
} public class Main {
	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
		BigInteger bordersum = BigInteger.ZERO;
		int tmpborder = 0;
		for (int i = 0; i < n; i++) {
			int tmp = sc.nextInt();
			if (i == 0) {
				tmpborder = tmp;
				continue;
			}
			if (tmp < tmpborder) {
				bordersum = bordersum.add(BigInteger.valueOf(tmpborder).subtract(BigInteger.valueOf(tmp)));
			} else {
				tmpborder = tmp;
			}
		}
		System.out.println(bordersum);
	}
} Main
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
		BigInteger bordersum = BigInteger.ZERO;
		int tmpborder = 0;
		for (int i = 0; i < n; i++) {
			int tmp = sc.nextInt();
			if (i == 0) {
				tmpborder = tmp;
				continue;
			}
			if (tmp < tmpborder) {
				bordersum = bordersum.add(BigInteger.valueOf(tmpborder).subtract(BigInteger.valueOf(tmp)));
			} else {
				tmpborder = tmp;
			}
		}
		System.out.println(bordersum);
	}
}
