#include <bits/stdc++.h>
using namespace std;
int main() {
  unsigned long int n, sum = 0, max = 0;
  cin >> n;
  vector<unsigned long int> a(n);
  cin >> a.at(0);
  for (unsigned long int i = 1; i < n; i++) {
    cin >> a.at(i);
    if (a.at(i) < a.at(i - 1)) {
      if (a.at(i - 1) <= max) {
        sum += max - a.at(i);
      } else {
        sum += a.at(i - 1) - a.at(i);
        max = a.at(i - 1);
      }
    }
  }
  cout << sum << endl;
}
