#include <bits/stdc++.h>
using namespace std;
int main() {
  long int n;
  long int v[200000];
  cin >> n;
  int stool = 0;
  for (long long int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  if (n == 1)
    cout << 0 << endl;
  else {
    for (long long int i = 1; i < n; ++i) {
      if (v[i] < v[i - 1]) {
        stool = stool + (v[i - 1] - v[i]);
      }
    }
    cout << stool << endl;
  }
  return 0;
}
