#include <bits/stdc++.h>
using namespace std;
int main() {
  unsigned long long N;
  cin >> N;
  if (N == 1) {
    cout << 0;
    return 0;
  }
  vector<unsigned long long> X(N);
  int sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> X[i];
  }
  for (int i = 0; i < X.size() - 1; i++) {
    if (X[i] - X[i + 1] > 0) {
      sum += X[i] - X[i + 1];
      X[i + 1] += X[i] - X[i + 1];
    }
  }
  cout << sum;
  return 0;
}
