#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll n;
  cin >> n;
  vector<ll> x;
  for (int i = 0; i < (int)(n); i++) {
    cin >> x[i];
  }
  ll ans = 0;
  for (int i = 0; i < (int)(n); i++) {
    if (x[i] > x[i + 1]) {
      ans += x[i] - x[i + 1];
      x[i] = x[i + 1];
    } else {
      continue;
    }
  }
  cout << ans << endl;
}
