#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i <= n - 1; i++) cin >> a[i];
  ll s = 0;
  if (n == 1) {
    cout << 0 << "\n";
  }
  int k = a[0];
  for (int i = 1; i <= n - 1; i++) {
    if (k <= a[i])
      k = a[i];
    else {
      s += k - a[i];
    }
  }
  cout << s << "\n";
  return 0;
}
