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