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