#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  vector<int> v(n);
  for (long long i = 0; i < n; i++) {
    cin >> v[i];
  }
  int ans = 0;
  int nmax = v[0];
  for (int i = 1; i < n; i++) {
    nmax = max(nmax, v[i]);
    if (v[i] < nmax) {
      ans += nmax - v[i];
      v[i] = nmax;
    }
  }
  cout << ans << '\n';
}
