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