#include <iostream>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <limits>
#include <string>
#include <algorithm>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include "math.h"
#include "assert.h"

using namespace std;

#define ll long long

#define BUFF ios::sync_with_stdio(false)
#define epsilon 1e-07
#define pi 2 * acos(0.0);
#define mod 1000000007   // 1e9+7
#define cerr if(false)cerr
#define watch(x) cerr << #x << " = " << x << endl;

#define xy pair<ll, ll>
#define x first
#define y second 

#define inf std::numeric_limits<ll>().max()

int main() {
	ll n;
	cin >> n;
	vector<ll> v(n); 
	for(ll i=0; i<n; i++) {
		cin >> v[i]; 
	}
	int ans = 0;
	int nmax = v[0];
	for(int i=1; i<n; i++) {
		nmax = max(nmax, v[i]);
		if (v[i] < nmax) {
			ans += nmax - v[i];
			v[i] = nmax;
		} 
	}
	cout << ans << '\n';
}

