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