#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  int heights = 0;
  int max_ = v[0];
  for (int i = 1; i < v.size(); i++) {
    if (v[i] < max_) heights += max_ - v[i];
    max_ = max(max_, v[i]);
  }
  cout << heights;
}
