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