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