#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto& i : a) cin >> i;
  ;
  int mx = a[0], ans = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] < mx) ans += mx - a[i];
    mx = max(mx, a[i]);
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  while (T--) solve();
  return 0;
}
