#include <bits/stdc++.h>
using namespace std;
int main()
{
	int a,b,now_max=-1;
	long long ans=0;
	cin>>a;
	for (int i=0;i<a;i++)
	{
		cin>>b;
		now_max=max(now_max,b);
		ans+=now_max-b;
	}
	cout<<ans<<endl;
	return 0;
}
