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