import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long max =sc.nextLong();
		long total =0;
		for (int i=0;i<N-1;i++) {
			long temp = sc.nextLong();
			if (temp<max) {
				total +=max-temp;
			} else {
				max = temp;
			}
		}
		System.out.println(total);
	}
}