// Problem Code: abc176_c

#include <bits/stdc++.h>

using namespace std;

long long C(int N, vector<int>& A) {
	long long cnt = 0, max_h = 0;
	for (int& x: A) {
		cnt += max(0, max_h - x);
		max_h = max(x, max_h);
	}
	return cnt;
}

int main() {
	int N;
	cin >> N;
	vector<int> A(N);
	for (int i = 0; i < N; i++)
		cin >> A[i];
	cout << C(N, A);
	return 0;
}