#include <iostream>
using namespace std;

int main()
{
	int N, A;
	long ans = 0;
	int pre = 0;

	cin >> N;

	for (int i = 0; i < N; i++)
	{
		cin >> A;
		if (pre > A) {
			ans += pre - A;
		}
		else {
			pre = A;
		}
	}

	cout << ans << endl;	

}