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