#include <algorithm>
#include <iostream>

using namespace std;

int main() {
	int n; cin >> n;
	int a_ = 0;
	long long ans = 0;
	while (n--) {
		int a; cin >> a;
		if (a_ > a)
			ans += a_ - a;
		else
			a_ = a;
	}
	cout << ans << '\n';
	return 0;
}
