#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long long n;
  cin >> n;
  vector<long long> a(n + 1);
  for (int i = 0; i < (int)(n); i++) {
    cin >> a[i];
  }
  int s = 0;
  for (int i = 1; i < n; i++) {
    if (a[i - 1] - a[i] > 0) {
      s += a[i - 1] - a[i];
      a[i] += a[i - 1] - a[i];
    }
  }
  cout << s << endl;
}
