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