import java.util.Scanner;
public class Main {
	public static int N;
	public static int[] A;
	public static int[] A_work;

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		N = Integer.parseInt(scan.next());
		A = new int[N];
		A_work = new int[N];
		for (int n = 0;n < N ;n++ ) {
			A[n] = Integer.parseInt(scan.next());
		}
		scan.close();

		System.out.println(search());
	}

	private static long search() {
		long sum = 0;
		for (int n = 0;n < N-1 ;n++ ) {
			int X = A[n] + A_work[n];
			int Y = A[n + 1] + A_work[n + 1];
			if (X > Y) {
				int diff = X - Y;
				A_work[n + 1]+=diff;
				Y+=diff;
				sum+=diff;
			}
		}
		return sum;
	}
}

/tmp/hf-datasets-cache/medium/datasets/42704881117311-config-parquet-and-info-qiankunmu-Project_CodeNet-896c9d36/hub/datasets--qiankunmu--Project_CodeNet_Python800_and_Java250/snapshots/c899fde44a77ba1cc14973ed3de064e4ff3aadc7/Project_CodeNet_Java250.tar.gz