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