#include <bits/stdc++.h>
using namespace std;
void edit(vector<int> &skill, int n) {
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    skill.push_back(x);
  }
}
void achilles() {
  int n;
  cin >> n;
  vector<int> h;
  edit(h, n);
  int ans = 0;
  int mx = 0;
  for (int i = 0; i < n; i++) {
    if (h[i] >= mx) {
      mx = h[i];
    } else {
      ans += (mx - h[i]);
    }
  }
  cout << ans << "\n";
}
int main() {
  achilles();
  return 0;
}
