#include <iostream>
#include <vector>
#include <string>
#include <bits/stdc++.h>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <deque>
#include <tuple>
#include <unordered_set>
#include <array>
#include <string>

using namespace std;
#define ll long long
#define ar array

int main() {
	int n;
	cin >> n;
	int a[n];
	for (int i=0; i<n; i++) cin >> a[i];
	int left[n+1]={}, ans=0;
	for (int i=0; i<n; i++) {
		if (left[i]>a[i]) ans+=left[i]-a[i];
		left[i+1]=max(left[i], a[i]);
	}
	cout << ans;
	return 0;
}