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