#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)n;i++)

int main() {
	int N;
	cin >> N;
	vector<int> A(N);
	rep(i, N) {
		cin >> A.at(i);
	}
	ll ans = 0;
	int MAXnum = 0;
	rep(i, N) {
		MAXnum = max(MAXnum, A[i]);
		int tmp = MAXnum;
		ans += tmp - A[i];
	}
	cout << ans << endl;
}