#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int sum = 0, now, past;
  cin >> past;
  for (int i = 1; i < n; ++i) {
    cin >> now;
    if (now < past) {
      sum += past - now;
      now = past;
    }
    past = now;
  }
  cout << sum << endl;
  return 0;
}
