#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
long long mod = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, pv, v;
  int res = 0;
  cin >> n;
  pv = 0;
  for (int i = 0; i < n; i++) {
    cin >> v;
    if (v < pv) {
      res += pv - v;
      v = pv;
    }
    pv = v;
  }
  cout << res;
}
