#include <bits/stdc++.h>
using namespace std;
template <typename Container>
bool include(const Container& c, const typename Container::value_type& v) {
  return (c.end() != std::find(c.begin(), c.end(), v));
}
template <class T>
vector<T> unique_vec(const vector<T>& v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  return v;
}
int main() {
  long long N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < (int)(N); i++) cin >> A[i];
  long long s = 0;
  for (int i = 0; i < (int)(N - 1); i++) {
    long long d = A[i + 1] - A[i];
    if (d < 0) {
      s += abs(d);
      A[i + 1] += abs(d);
    }
  }
  cout << s << endl;
  return 0;
}
