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