#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int niz[n];
  for (auto& a : niz) {
    cin >> a;
  }
  int zbr = 0;
  for (int i = 0; i < n - 1; i++) {
    if (niz[i] > niz[i + 1]) {
      zbr += niz[i] - niz[i + 1];
      niz[i + 1] = niz[i];
    }
  }
  cout << zbr << endl;
}
