#include <iostream>

using namespace std;

int main(){
	long long n = 0, count = 0, last = 0;
	cin >> n;
	for(int i = 0;i < n; ++i){
		int x;
		cin >> x;
		if(x >= last){
			last = x;
			}
		else{
			count += (last - x);
			}
		}
	cout << count;
	return 0;
	}
