#include <stdio.h>

int main () {

	int N;
	int A[10000000];
	
	scanf("%d", &N);
	for(int Ai = 0; Ai < N; Ai++) {
		scanf("%d", &A[Ai]);
	}
	
	int total_hight = 0;
	int tmp = A[0];
	for(int Ai = 1; A[Ai] != 0; ++Ai) {
		if(tmp > A[Ai]) {
			//printf("A[Ai + 1]= %d\n", A[Ai + 1]);
			//printf("total_hight= %d\n", A[Ai] - A[Ai + 1]);
			total_hight += tmp - A[Ai];
			total_hight = A[Ai]
		}
	}
	
	printf("%d", total_hight);
	return 0;
}
