#include <bits/stdc++.h>
using namespace std;
int a[100005];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int worst = 0;
  int need = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] < worst) {
      need += (worst - a[i]);
      a[i] = worst;
    }
    worst = max(worst, a[i]);
  }
  cout << need << endl;
}
