import java.util.ArrayList;
import java.util.Scanner;
 
public class Main {
 
	public static void main(String[] args) {
 
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
 
		ArrayList<Long> array = new ArrayList<>();
 
		for (int i = 0; i < n; i++) {
			array.add(sc.nextLong());
		}
 
		long sum = 0;
		for (int j = 0; j < n - 1; j++) {
			long diff = array.get(j) - array.get(j + 1);
			if (diff > 0) {
				sum += diff;
				array.set(j + 1, array.get(j + 1) + diff);
			}
		}
 
		System.out.println(sum);
 
	}
} import java.util.ArrayList; ArrayList java.util import java.util.Scanner; Scanner java.util public class Main {
 
	public static void main(String[] args) {
 
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
 
		ArrayList<Long> array = new ArrayList<>();
 
		for (int i = 0; i < n; i++) {
			array.add(sc.nextLong());
		}
 
		long sum = 0;
		for (int j = 0; j < n - 1; j++) {
			long diff = array.get(j) - array.get(j + 1);
			if (diff > 0) {
				sum += diff;
				array.set(j + 1, array.get(j + 1) + diff);
			}
		}
 
		System.out.println(sum);
 
	}
} Main public static void main(String[] args) {
 
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
 
		ArrayList<Long> array = new ArrayList<>();
 
		for (int i = 0; i < n; i++) {
			array.add(sc.nextLong());
		}
 
		long sum = 0;
		for (int j = 0; j < n - 1; j++) {
			long diff = array.get(j) - array.get(j + 1);
			if (diff > 0) {
				sum += diff;
				array.set(j + 1, array.get(j + 1) + diff);
			}
		}
 
		System.out.println(sum);
 
	} main {
 
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
 
		ArrayList<Long> array = new ArrayList<>();
 
		for (int i = 0; i < n; i++) {
			array.add(sc.nextLong());
		}
 
		long sum = 0;
		for (int j = 0; j < n - 1; j++) {
			long diff = array.get(j) - array.get(j + 1);
			if (diff > 0) {
				sum += diff;
				array.set(j + 1, array.get(j + 1) + diff);
			}
		}
 
		System.out.println(sum);
 
	} 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 ArrayList<Long> array = new ArrayList<>(); array new ArrayList<>() for (int i = 0; i < n; i++) {
			array.add(sc.nextLong());
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			array.add(sc.nextLong());
		} {
			array.add(sc.nextLong());
		} array.add(sc.nextLong()) array.add array sc.nextLong() sc.nextLong sc long sum = 0; sum 0 for (int j = 0; j < n - 1; j++) {
			long diff = array.get(j) - array.get(j + 1);
			if (diff > 0) {
				sum += diff;
				array.set(j + 1, array.get(j + 1) + diff);
			}
		} int j = 0; int j = 0; j 0 j < n - 1 j n - 1 n 1 j++ j++ j {
			long diff = array.get(j) - array.get(j + 1);
			if (diff > 0) {
				sum += diff;
				array.set(j + 1, array.get(j + 1) + diff);
			}
		} {
			long diff = array.get(j) - array.get(j + 1);
			if (diff > 0) {
				sum += diff;
				array.set(j + 1, array.get(j + 1) + diff);
			}
		} long diff = array.get(j) - array.get(j + 1); diff array.get(j) - array.get(j + 1) array.get(j) array.get array j array.get(j + 1) array.get array j + 1 j 1 if (diff > 0) {
				sum += diff;
				array.set(j + 1, array.get(j + 1) + diff);
			} diff > 0 diff 0 {
				sum += diff;
				array.set(j + 1, array.get(j + 1) + diff);
			} sum += diff sum diff array.set(j + 1, array.get(j + 1) + diff) array.set array j + 1 j 1 array.get(j + 1) + diff array.get(j + 1) array.get array j + 1 j 1 diff 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) {
 
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
 
		ArrayList<Long> array = new ArrayList<>();
 
		for (int i = 0; i < n; i++) {
			array.add(sc.nextLong());
		}
 
		long sum = 0;
		for (int j = 0; j < n - 1; j++) {
			long diff = array.get(j) - array.get(j + 1);
			if (diff > 0) {
				sum += diff;
				array.set(j + 1, array.get(j + 1) + diff);
			}
		}
 
		System.out.println(sum);
 
	}
} public class Main {
 
	public static void main(String[] args) {
 
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
 
		ArrayList<Long> array = new ArrayList<>();
 
		for (int i = 0; i < n; i++) {
			array.add(sc.nextLong());
		}
 
		long sum = 0;
		for (int j = 0; j < n - 1; j++) {
			long diff = array.get(j) - array.get(j + 1);
			if (diff > 0) {
				sum += diff;
				array.set(j + 1, array.get(j + 1) + diff);
			}
		}
 
		System.out.println(sum);
 
	}
} Main
import java.util.ArrayList;
import java.util.Scanner;
 
public class Main {
 
	public static void main(String[] args) {
 
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
 
		ArrayList<Long> array = new ArrayList<>();
 
		for (int i = 0; i < n; i++) {
			array.add(sc.nextLong());
		}
 
		long sum = 0;
		for (int j = 0; j < n - 1; j++) {
			long diff = array.get(j) - array.get(j + 1);
			if (diff > 0) {
				sum += diff;
				array.set(j + 1, array.get(j + 1) + diff);
			}
		}
 
		System.out.println(sum);
 
	}
}