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