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