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