#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
	int N;
	cin >> N;
	vector<int> A(N);
	for (int i = 0; i < N; i++)
	{
		cin >> A[i];
	}

	long long ans = 0;
	int MaxNum = 0;
	for (int i = 0; i < N-1; i++)
	{
      if(A[i]>=A[i+1])
		ans += A[i]-A[i+1];
      A[i+1]=A[i]
	}

	cout << ans << endl;
}


