#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < (n); ++i) cin >> a[i];
  int sum = 0, now = a[0];
  for (int i = 0; i < (n); ++i) {
    int tmp = now - a[i];
    if (tmp > 0) {
      sum += tmp;
    }
    chmax(now, a[i]);
  }
  cout << sum << endl;
  return 0;
}
