#include <bits/stdc++.h>
using namespace std;
int main() {
  double N;
  cin >> N;
  vector<double> A(N);
  for (double i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  double ans = 0;
  for (double i = 1; i < N; i++) {
    double dlt = A.at(i) - A.at(i - 1);
    if (dlt < 0) {
      ans -= dlt;
      A.at(i) = A.at(i) - dlt;
    }
  }
  cout << ans << endl;
}
