#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
void solve() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) cin >> v[i];
  ll ans = 0, mx = v[0];
  for (int i = 1; i < n; i++) {
    if (v[i] < v[i - 1]) {
      ans += abs(mx - v[i]);
    } else
      mx = v[i];
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  t = 1;
  while (t--) {
    solve();
  }
}
