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

int main()
{
	int n; cin >> n;
	int pre; cin >> pre;
	int num;
	long long int req = 0;
	for(int i = 1; i < n; ++i)
	{
		cin >> num;
		req += max(0, pre - num);
		pre = max(pre, num);
	}
	cout << req;
}