#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &f) {
  f = 0;
  T x = 1;
  char ch = getchar();
  while (ch < 48 || ch > 57) {
    if (ch == '-') {
      x = -1;
    }
    ch = getchar();
  }
  while (ch <= 57 && ch >= 48) {
    f = f * 10 + ch - '0';
    ch = getchar();
  }
  f *= x;
}
long long a[100005];
long long n;
int main() {
  read(n);
  for (long long i = 1; i <= n; i++) {
    read(a[i]);
  }
  long long ans = 0;
  for (long long i = 2; i <= n; i++) {
    if (a[i] >= a[i - 1]) {
      continue;
    } else {
      ans += a[i - 1] - a[i];
    }
  }
  cout << ans << endl;
  return 0;
}
