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