#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, count = 0, i, f;
  cin >> n;
  int *p = new int[n];
  for (i = 0; i < n; i++) {
    cin >> p[i];
  }
  f = p[0];
  for (i = 1; i < n; i++) {
    if (p[i] < f) {
      count = count + f - p[i];
      p[i] = f;
    }
    f = p[i];
  }
  cout << count;
  return 0;
}
