#include <cstdio>

int main() {
	int n;
	long long sum = 0;
	scanf ("%d", &n);
	int last, x;
	for(int i = 1; i <= n; i++) {
		scanf ("%d", &x);
		if(x < last) {
			sum += last - x;	
			x = last;		
		}
		last = x;
	}
	printf("%lld\n", sum);
	return 0;
}