#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main()
{
	ll n, c, ans=0, m=0;
	cin >> n;
	for (ll i=0; i<n; i++)
	{
		cin >> c;
		m = max(m, c);
		ans += m - c;

	}
	cout<<ans<<"\n";

	return 0;
}