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