#include <bits/stdc++.h>
using namespace std;
template <typename T>
void prarr(T a) {
  cerr << "[ ";
  for (auto i : a) {
    cerr << i << " ";
  }
  cerr << "]" << endl;
}
template <typename Arg1>
void _er(const char* name, Arg1&& arg1) {
  cerr << "[" << name << " : " << arg1 << "]" << endl;
}
template <typename Arg1, typename... Args>
void _er(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr << "[";
  cerr.write(names, comma - names) << " : " << arg1 << "] ";
  _er(comma + 2, args...);
}
void solve() {
  long long int n;
  cin >> n;
  long long int A[n + 1];
  long long int sum = 0;
  for (long long int i = 1; i <= n; i++) {
    cin >> A[i];
    if (i > 1) {
      sum += max(0LL, (A[i - 1] - A[i]));
    }
  }
  cout << sum << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
