#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long sum = 0;
  long long best = a[0];
  for (long long i = 1; i < n; i++) {
    if (a[i] < best) {
      sum += abs(best - a[i]);
      best = a[i];
    }
  }
  cout << sum << "\n";
  return 0;
}
