#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  long long sm = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i >= 1) {
      long long dis = a[i] - a[i - 1];
      if (dis > 0) {
        sm = sm + dis;
        a[i] = a[i] + dis;
      }
    }
  }
  cout << sm << endl;
}
