#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  long long head, tail, term = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    tail = head;
    cin >> head;
    if (i > 0 && head < tail) term += tail - head;
  }
  cout << term;
}
