#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
	int N;
	cin >> N;
	vector<int> A(N);
	for (auto& x : A) cin >> x;
	int m = 0;
	ll r = 0;
	for (auto x : A) {
		m = max(m, x);
		r += m - x;
	}
	cout << r << endl;
}
