#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  if (n == 0 || n == 1) {
    cout << 0;
    return 0;
  }
  vector<int> a(n);
  for (vector<int>::iterator i = a.begin(); i != a.end(); i++) cin >> *i;
  int ans = 0;
  for (vector<int>::iterator i = a.begin() + 1; i != a.end(); i++) {
    if (*(i - 1) > *(i)) {
      ans += *(i - 1) - *(i);
      *(i) = *(i - 1);
    }
  }
  cout << ans;
  return 0;
}
