#include <iostream>
using namespace std;
int main(){
	long long N,f = 0,ans = 0;
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		int a;
		cin >> a;
		if(f > a) ans += (f-a);
		else f = a;
	}
	cout << ans << endl;
}