#include<cstdio>
int n,x,lastans;
long long ans;
int main()
{
	scanf("%d",&n);
	scanf("%d",&lastans);
	for(int i=2;i<=n;i++) scanf("%d",&x),ans+=lastans>x?lastans-x:0,lastans=x<lastans?lastans:x;
	printf("%lld",ans);
	return 0;
}