#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double epsilon = 1e-7;
signed main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  int mx = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (v[i] < mx) {
      ans += (mx - v[i]);
    } else {
      mx = v[i];
    }
  }
  cout << ans << '\n';
  return 0;
}
