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