import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long[] height =new long[N];
		long con = 0;

		for (int i = 0; i < N; i++) {
			height[i] = sc.nextLong();
		}

		for (int i = 1; i < N; i++) {
			if (height[i-1] > height[i]) {
				long let =height[i-1] - height[i];
				height[i] += let;
				con += let;
			}
		}

		System.out.println(con);


	}

} 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();
		long[] height =new long[N];
		long con = 0;

		for (int i = 0; i < N; i++) {
			height[i] = sc.nextLong();
		}

		for (int i = 1; i < N; i++) {
			if (height[i-1] > height[i]) {
				long let =height[i-1] - height[i];
				height[i] += let;
				con += let;
			}
		}

		System.out.println(con);


	}

} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long[] height =new long[N];
		long con = 0;

		for (int i = 0; i < N; i++) {
			height[i] = sc.nextLong();
		}

		for (int i = 1; i < N; i++) {
			if (height[i-1] > height[i]) {
				long let =height[i-1] - height[i];
				height[i] += let;
				con += let;
			}
		}

		System.out.println(con);


	} main {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long[] height =new long[N];
		long con = 0;

		for (int i = 0; i < N; i++) {
			height[i] = sc.nextLong();
		}

		for (int i = 1; i < N; i++) {
			if (height[i-1] > height[i]) {
				long let =height[i-1] - height[i];
				height[i] += let;
				con += let;
			}
		}

		System.out.println(con);


	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc long[] height =new long[N]; height new long[N] N long con = 0; con 0 for (int i = 0; i < N; i++) {
			height[i] = sc.nextLong();
		} int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
			height[i] = sc.nextLong();
		} {
			height[i] = sc.nextLong();
		} height[i] = sc.nextLong() height[i] height i sc.nextLong() sc.nextLong sc for (int i = 1; i < N; i++) {
			if (height[i-1] > height[i]) {
				long let =height[i-1] - height[i];
				height[i] += let;
				con += let;
			}
		} int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
			if (height[i-1] > height[i]) {
				long let =height[i-1] - height[i];
				height[i] += let;
				con += let;
			}
		} {
			if (height[i-1] > height[i]) {
				long let =height[i-1] - height[i];
				height[i] += let;
				con += let;
			}
		} if (height[i-1] > height[i]) {
				long let =height[i-1] - height[i];
				height[i] += let;
				con += let;
			} height[i-1] > height[i] height[i-1] height i-1 i 1 height[i] height i {
				long let =height[i-1] - height[i];
				height[i] += let;
				con += let;
			} long let =height[i-1] - height[i]; let height[i-1] - height[i] height[i-1] height i-1 i 1 height[i] height i height[i] += let height[i] height i let con += let con let System.out.println(con) System.out.println System.out System System.out con String[] args args public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long[] height =new long[N];
		long con = 0;

		for (int i = 0; i < N; i++) {
			height[i] = sc.nextLong();
		}

		for (int i = 1; i < N; i++) {
			if (height[i-1] > height[i]) {
				long let =height[i-1] - height[i];
				height[i] += let;
				con += let;
			}
		}

		System.out.println(con);


	}

} public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long[] height =new long[N];
		long con = 0;

		for (int i = 0; i < N; i++) {
			height[i] = sc.nextLong();
		}

		for (int i = 1; i < N; i++) {
			if (height[i-1] > height[i]) {
				long let =height[i-1] - height[i];
				height[i] += let;
				con += let;
			}
		}

		System.out.println(con);


	}

} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long[] height =new long[N];
		long con = 0;

		for (int i = 0; i < N; i++) {
			height[i] = sc.nextLong();
		}

		for (int i = 1; i < N; i++) {
			if (height[i-1] > height[i]) {
				long let =height[i-1] - height[i];
				height[i] += let;
				con += let;
			}
		}

		System.out.println(con);


	}

}