#include <stdio.h>

int main () {

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