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