#include <iostream>

using namespace std;

int main() {
 	ing n;
  	cin >> n;
  	long long arr[n];
  
  	int sum = 0;
  	for (int i = 1; i < n; i++) {
     	if (arr[i - 1] > arr[i]) sum += (arr[i - 1] - arr[i]);
    }
  
  	cout << sum << endl;
}