#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int n;
  cin >> n;
  vector<long long int> v(n);
  long long int mx, x, ans;
  cin >> mx;
  for (long long int i = 1; i < n; i++) {
    cin >> x;
    ans += max(0LL, mx - x);
    mx = max(x, mx);
  }
  cout << ans << endl;
}
signed main() {
  solve();
  return 0;
}
