import java.util.Scanner;

public class Main{

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.nextLine());
		String[] tmp = sc.nextLine().split(" ");
		long[] list = new long[n];
		long ans   = 0;


		list[0] = Long.parseLong(tmp[0]);
		for(int i = 1; i < n; i++) {

			list[i] = Long.parseLong(tmp[i]);

			if(list[i - 1] > list[i]) {

				ans += list[i - 1] - list[i];
				list[i] = list[i - 1];

			}
		}

		System.out.println(ans);
	}

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

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.nextLine());
		String[] tmp = sc.nextLine().split(" ");
		long[] list = new long[n];
		long ans   = 0;


		list[0] = Long.parseLong(tmp[0]);
		for(int i = 1; i < n; i++) {

			list[i] = Long.parseLong(tmp[i]);

			if(list[i - 1] > list[i]) {

				ans += list[i - 1] - list[i];
				list[i] = list[i - 1];

			}
		}

		System.out.println(ans);
	}

} Main public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.nextLine());
		String[] tmp = sc.nextLine().split(" ");
		long[] list = new long[n];
		long ans   = 0;


		list[0] = Long.parseLong(tmp[0]);
		for(int i = 1; i < n; i++) {

			list[i] = Long.parseLong(tmp[i]);

			if(list[i - 1] > list[i]) {

				ans += list[i - 1] - list[i];
				list[i] = list[i - 1];

			}
		}

		System.out.println(ans);
	} main {

		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.nextLine());
		String[] tmp = sc.nextLine().split(" ");
		long[] list = new long[n];
		long ans   = 0;


		list[0] = Long.parseLong(tmp[0]);
		for(int i = 1; i < n; i++) {

			list[i] = Long.parseLong(tmp[i]);

			if(list[i - 1] > list[i]) {

				ans += list[i - 1] - list[i];
				list[i] = list[i - 1];

			}
		}

		System.out.println(ans);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = Integer.parseInt(sc.nextLine()); n Integer.parseInt(sc.nextLine()) Integer.parseInt Integer sc.nextLine() sc.nextLine sc String[] tmp = sc.nextLine().split(" "); tmp sc.nextLine().split(" ") sc.nextLine().split sc.nextLine() sc.nextLine sc " " long[] list = new long[n]; list new long[n] n long ans   = 0; ans 0 list[0] = Long.parseLong(tmp[0]) list[0] list 0 Long.parseLong(tmp[0]) Long.parseLong Long tmp[0] tmp 0 for(int i = 1; i < n; i++) {

			list[i] = Long.parseLong(tmp[i]);

			if(list[i - 1] > list[i]) {

				ans += list[i - 1] - list[i];
				list[i] = list[i - 1];

			}
		} int i = 1; int i = 1; i 1 i < n i n i++ i++ i {

			list[i] = Long.parseLong(tmp[i]);

			if(list[i - 1] > list[i]) {

				ans += list[i - 1] - list[i];
				list[i] = list[i - 1];

			}
		} {

			list[i] = Long.parseLong(tmp[i]);

			if(list[i - 1] > list[i]) {

				ans += list[i - 1] - list[i];
				list[i] = list[i - 1];

			}
		} list[i] = Long.parseLong(tmp[i]) list[i] list i Long.parseLong(tmp[i]) Long.parseLong Long tmp[i] tmp i if(list[i - 1] > list[i]) {

				ans += list[i - 1] - list[i];
				list[i] = list[i - 1];

			} list[i - 1] > list[i] list[i - 1] list i - 1 i 1 list[i] list i {

				ans += list[i - 1] - list[i];
				list[i] = list[i - 1];

			} ans += list[i - 1] - list[i] ans list[i - 1] - list[i] list[i - 1] list i - 1 i 1 list[i] list i list[i] = list[i - 1] list[i] list i list[i - 1] list i - 1 i 1 System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main{

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.nextLine());
		String[] tmp = sc.nextLine().split(" ");
		long[] list = new long[n];
		long ans   = 0;


		list[0] = Long.parseLong(tmp[0]);
		for(int i = 1; i < n; i++) {

			list[i] = Long.parseLong(tmp[i]);

			if(list[i - 1] > list[i]) {

				ans += list[i - 1] - list[i];
				list[i] = list[i - 1];

			}
		}

		System.out.println(ans);
	}

} public class Main{

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.nextLine());
		String[] tmp = sc.nextLine().split(" ");
		long[] list = new long[n];
		long ans   = 0;


		list[0] = Long.parseLong(tmp[0]);
		for(int i = 1; i < n; i++) {

			list[i] = Long.parseLong(tmp[i]);

			if(list[i - 1] > list[i]) {

				ans += list[i - 1] - list[i];
				list[i] = list[i - 1];

			}
		}

		System.out.println(ans);
	}

} Main
import java.util.Scanner;

public class Main{

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.nextLine());
		String[] tmp = sc.nextLine().split(" ");
		long[] list = new long[n];
		long ans   = 0;


		list[0] = Long.parseLong(tmp[0]);
		for(int i = 1; i < n; i++) {

			list[i] = Long.parseLong(tmp[i]);

			if(list[i - 1] > list[i]) {

				ans += list[i - 1] - list[i];
				list[i] = list[i - 1];

			}
		}

		System.out.println(ans);
	}

}
