#include<bits/stdc++.h>
#define st first
#define nd second
#define PQ priority_queue

using ll = long long ;
using namespace std;
using pii = pair<int,int>;

int main(){
	int n;
	ll sum = 0;
	scanf("%d", &n);
	int x, pre = 0;
	for(int i = 1; i <= n; i++){
		scanf("%d", &x);
		if(x < pre){
			sum += pre - x;
			x = pre;
		}
		pre = x;
	}
	printf("%lld", sum);


}#include<bits/stdc++.h>
#define st first
#define nd second
#define PQ priority_queue

using ll = long long ;
using namespace std;
using pii = pair<int,int>;

int main(){
	int n;
	ll sum = 0;
	scanf("%d", &n);
	int x, pre = 0;
	for(int i = 1; i <= n; i++){
		scanf("%d", &x);
		if(x < pre){
			sum += pre - x;
			x = pre;
		}
		pre = x;
	}
	printf("%lld", sum);


}