#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, last, a;
  long long cnt = 0;
  cin >> n >> last;
  for (int i = 1; i < n; i++) {
    cin >> a;
    cnt += max(0, last - a);
    last = a;
  }
  cout << cnt;
  return 0;
}
