#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  unsigned int sum = 0, tmp, ptmp;
  cin >> ptmp;
  for (int i = 1; i < n; ++i) {
    cin >> tmp;
    if (ptmp > tmp) {
      sum += (ptmp - tmp);
      tmp += (ptmp - tmp);
    }
    ptmp = tmp;
  }
  cout << sum << endl;
  return 0;
}
