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