#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n,x,mx;
long long ans=0;
int main(){
	scanf("%d",&n);
	while(n--){
		scanf("%d",&x);
		ans+=x>=mx?0:mx-x;
		mx=max(mx,x);
	}
	printf("%lld\n",ans);
	return 0;
}