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