#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> xs(n);
  for (int i = 0; i < n; i++) cin >> xs[i];
  int max_until_now = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (xs[i] < max_until_now) ans += max_until_now - xs[i];
    max_until_now = max(max_until_now, xs[i]);
  }
  cout << ans << endl;
  return 0;
}
