#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <unordered_map>
#include <limits>
#include <cassert>

using namespace std;

#define inf numeric_limits<int>::max()
#define double_inf numeric_limits<double>::infinity();

typedef long long int lli;
typedef unsigned long long int ulli;


int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	int N = 0;
	lli loc_max = 0;
	ulli total_sum = 0;
	vector<int> A;
	cin >> N;
	A.resize(N);
	for (int i = 0; i < N; i++){
		cin >> A[i];
		loc_max = max(A[i], loc_max);
		total_sum += (loc_max>A[i] ? loc_max-A[i] : 0);
	}
	cout << total_sum << endl;
	return 0;
}