#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;
  for (int i = 1; i < t; i++) {
    if (a[i] < a[i - 1]) {
      int d = a[i - 1] - a[i];
      s += d;
      a[i] += d;
    }
  }
  cout << s << endl;
  return 0;
}
