#include <cstdio>
int main(){
	int n,x;
	long long sum = 0, curr = 0;
	scanf("%d", &n);
	for(int i=0;i<n;i++){
		scanf("%d", &x);
		if(curr > x){
			sum += curr - x;
		}else{
			curr = x;
		}
	}
	printf("%lld\n", sum);
	return 0;
}
