#include <bits/stdc++.h>
using namespace std;
int main() {
  long int n, x = 0;
  long long int v[200000];
  cin >> n;
  int stool = 0;
  for (long int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  for (long int i = 1; i < n; ++i) {
    if (v[i] < v[i - 1]) {
      stool = stool + (v[i - 1] - v[i]);
    }
  }
  cout << stool;
  return 0;
}
