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