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

int main() {
	int n, m = 0; cin >> n;
	long long s = 0;
	while(n--) {
		int x; cin >> x;
		if(x >= m) m = x;
		else s += m - x;
	}
	cout << s;
}
