#include <bits/stdc++.h>

#define ll long long

using namespace std;

int main()
{
	int n;
	cin >> n;
	ll last=0,sum=0;
	for(int i = 0; i < n; i++)
	{
		ll ai;
		cin >> ai;
		sum += max(0ll, last-ai);
		last=max(last,ai);
	}
	cout << sum << endl;
}
