#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  
  long long sum = 0;
  int max_height = 0;
  for (int i = 0; i < n; i++) {
    if (a.at(i) < max_height) {
      sum += max_height - a.at(i);
    }
    man_height = max(max_height, a.at(i));
  }
  cout << sum << endl;
}
