#include <iostream>
using namespace std;

int main() {
	long long n, x, p = -1, ans = 0;
	cin >> n;
	for (long long i = 1; i <= n; i++) {
		cin >> x;
		if (p > x) {
			ans += p - x;
			x = p;
		}
		p = x;
	}
	cout << ans << endl;
	return 0;
}
