#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  int d = 0;
  int cnt = 0;
  for (int i = 0; i < (int)(n); i++) {
    cin >> a.at(i);
    if (a.at(i) < d) {
      cnt += d - a.at(i);
    }
    if (d < a.at(i)) {
      d = a.at(i);
    }
  }
  cout << cnt << endl;
}
