#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[ ";
  for (auto a : v) os << a << " ";
  os << "]";
  return os;
}
int main() {
  cout << fixed << setprecision(0);
  int N;
  cin >> N;
  vector<int> A(N);
  for (auto &a : A) cin >> a;
  int ans = 0;
  int sh = A[0];
  for (int i = 1; i < N; i++) {
    if (A[i] < sh) {
      ans += sh - A[i];
    }
    sh = max(sh, A[i]);
  }
  cout << ans << endl;
  return 0;
}
