#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int stepSum = 0;
  cin >> n;
  vector<int> tall(n);
  for (int i = 0; i < n; i++) {
    cin >> tall.at(i);
  }
  if (tall.size() == 1) {
    cout << stepSum << endl;
  } else {
    for (int n = 0; n < n; n++) {
      if (tall.at(n) < tall.at(n - 1)) {
        stepSum += tall.at(n - 1) - tall.at(n);
      }
    }
  }
  cout << stepSum << endl;
}
