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