#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  vector<int> vec;
  string s;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s;
    vec.push_back(atoi(s.c_str()));
  }
  int result = 0;
  for (int i = 1; i < vec.size(); i++) {
    int buf = vec[i - 1] - vec[i];
    if (buf > 0) {
      result += buf;
      vec[i] += buf;
    }
  }
  cout << result << endl;
  return 0;
}
