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