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