#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, counter = 0;
  cin >> n;
  long long arr[n + 5];
  for (int i = 0; i < n; i++) cin >> arr[i];
  for (int i = 0; i < n; i++) {
    if (i != 0) {
      if (arr[i] < arr[i - 1]) {
        counter += arr[i - 1] - arr[i];
      }
    }
  }
  cout << counter << endl;
}
