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