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