#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  long long int min, s = 0;
  cin >> n;
  long long int a[n];
  for (long long int ii = 0; ii < n; ii++) cin >> a[ii];
  min = a[0];
  for (long long int ii = 1; ii < n; ii++) {
    if (a[ii] < a[ii - 1]) {
      s += abs(min - a[ii]);
      a[ii] += abs(min - a[ii]);
    }
  }
  cout << s << '\n';
}
