#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  vector<int> v(t);
  vector<int> a(t);
  for (int i = 0; i < t; i++) {
    cin >> a[i];
  }
  int sum = 0;
  for (int j = 0; j < t - 1; j++) {
    if (a[j] > a[j + 1]) {
      v[j] = 0;
    } else {
      v[j] = a[j + 1] - a[j];
      sum += v[j];
    }
  }
  cout << sum << endl;
  return 0;
}
