#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  vector<int> a(t);
  for (int i = 0; i < t; i++) {
    cin >> a[i];
  }
  int s = 0;
  int worst = 0;
  for (int i = 0; i < t; i++) {
    if (a[i] < worst) {
      s += worst - a[i];
      a[i] = worst;
    }
    worst = max(worst, a[i]);
  }
  cout << s << endl;
  return 0;
}
