import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();

		long[] array = new long[N];

		long sum = 0;
		long num = 0;
		for(int i = 0; i < N; i++) {
			array[i] = scan.nextLong();
			if(i != 0) {
				num = array[i] - array[i-1];
				if(num < 0) {
					array[i] = array[i] + Math.abs(num);
					sum = sum + Math.abs(num);
				}
			}
		}
		System.out.println(sum);
	}

} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();

		long[] array = new long[N];

		long sum = 0;
		long num = 0;
		for(int i = 0; i < N; i++) {
			array[i] = scan.nextLong();
			if(i != 0) {
				num = array[i] - array[i-1];
				if(num < 0) {
					array[i] = array[i] + Math.abs(num);
					sum = sum + Math.abs(num);
				}
			}
		}
		System.out.println(sum);
	}

} Main public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();

		long[] array = new long[N];

		long sum = 0;
		long num = 0;
		for(int i = 0; i < N; i++) {
			array[i] = scan.nextLong();
			if(i != 0) {
				num = array[i] - array[i-1];
				if(num < 0) {
					array[i] = array[i] + Math.abs(num);
					sum = sum + Math.abs(num);
				}
			}
		}
		System.out.println(sum);
	} main {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();

		long[] array = new long[N];

		long sum = 0;
		long num = 0;
		for(int i = 0; i < N; i++) {
			array[i] = scan.nextLong();
			if(i != 0) {
				num = array[i] - array[i-1];
				if(num < 0) {
					array[i] = array[i] + Math.abs(num);
					sum = sum + Math.abs(num);
				}
			}
		}
		System.out.println(sum);
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int N = scan.nextInt(); N scan.nextInt() scan.nextInt scan long[] array = new long[N]; array new long[N] N long sum = 0; sum 0 long num = 0; num 0 for(int i = 0; i < N; i++) {
			array[i] = scan.nextLong();
			if(i != 0) {
				num = array[i] - array[i-1];
				if(num < 0) {
					array[i] = array[i] + Math.abs(num);
					sum = sum + Math.abs(num);
				}
			}
		} int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
			array[i] = scan.nextLong();
			if(i != 0) {
				num = array[i] - array[i-1];
				if(num < 0) {
					array[i] = array[i] + Math.abs(num);
					sum = sum + Math.abs(num);
				}
			}
		} {
			array[i] = scan.nextLong();
			if(i != 0) {
				num = array[i] - array[i-1];
				if(num < 0) {
					array[i] = array[i] + Math.abs(num);
					sum = sum + Math.abs(num);
				}
			}
		} array[i] = scan.nextLong() array[i] array i scan.nextLong() scan.nextLong scan if(i != 0) {
				num = array[i] - array[i-1];
				if(num < 0) {
					array[i] = array[i] + Math.abs(num);
					sum = sum + Math.abs(num);
				}
			} i != 0 i 0 {
				num = array[i] - array[i-1];
				if(num < 0) {
					array[i] = array[i] + Math.abs(num);
					sum = sum + Math.abs(num);
				}
			} num = array[i] - array[i-1] num array[i] - array[i-1] array[i] array i array[i-1] array i-1 i 1 if(num < 0) {
					array[i] = array[i] + Math.abs(num);
					sum = sum + Math.abs(num);
				} num < 0 num 0 {
					array[i] = array[i] + Math.abs(num);
					sum = sum + Math.abs(num);
				} array[i] = array[i] + Math.abs(num) array[i] array i array[i] + Math.abs(num) array[i] array i Math.abs(num) Math.abs Math num sum = sum + Math.abs(num) sum sum + Math.abs(num) sum Math.abs(num) Math.abs Math num System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();

		long[] array = new long[N];

		long sum = 0;
		long num = 0;
		for(int i = 0; i < N; i++) {
			array[i] = scan.nextLong();
			if(i != 0) {
				num = array[i] - array[i-1];
				if(num < 0) {
					array[i] = array[i] + Math.abs(num);
					sum = sum + Math.abs(num);
				}
			}
		}
		System.out.println(sum);
	}

} public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();

		long[] array = new long[N];

		long sum = 0;
		long num = 0;
		for(int i = 0; i < N; i++) {
			array[i] = scan.nextLong();
			if(i != 0) {
				num = array[i] - array[i-1];
				if(num < 0) {
					array[i] = array[i] + Math.abs(num);
					sum = sum + Math.abs(num);
				}
			}
		}
		System.out.println(sum);
	}

} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();

		long[] array = new long[N];

		long sum = 0;
		long num = 0;
		for(int i = 0; i < N; i++) {
			array[i] = scan.nextLong();
			if(i != 0) {
				num = array[i] - array[i-1];
				if(num < 0) {
					array[i] = array[i] + Math.abs(num);
					sum = sum + Math.abs(num);
				}
			}
		}
		System.out.println(sum);
	}

}