#include <bits/stdc++.h>
using namespace std;
long long x[1000000], y[1000000], a[1000000];
int main() {
  long long b, c, d, e, f, i, j, n, p;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  int count = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] < a[i - 1]) {
      int p = (a[i - 1] - a[i]);
      count += p;
      a[i] += p;
    }
  }
  cout << count << endl;
}
