#include <bits/stdc++.h>
using namespace std;
long long int num[200005];
int main(void) {
  int m, n, i, j, k = 0, d, pre = 0, now;
  string s;
  cin >> n;
  cin >> pre;
  for (i = 1; i < n; i++) {
    cin >> now;
    if (pre > now) k += pre - now;
    pre = max(pre, now);
  }
  cout << k;
  return 0;
}
