#include <bits/stdc++.h>
using namespace std;
long long GCD(long long x, long long y) { return y ? GCD(y, x % y) : x; }
int main() {
  long long N;
  cin >> N;
  long long su = 0;
  long long p = 0;
  vector<long long> A(N);
  for (long long i = 0; i < N; ++i) cin >> A[i];
  for (int i = 0; i < N; ++i) {
    if (A[p] > A[i]) {
      su += A[p] - A[i];
    } else
      p = A[i];
  }
  cout << su << endl;
  return 0;
}
