#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int i, n;
  cin >> n;
  vector<int> a(n);
  for (i = 0; i < n; ++i) cin >> a[i];
  int cnt = 0;
  while (--n)
    if (a[n] > a[n - 1]) cnt += a[n] - a[n - 1];
  cout << cnt << endl;
  return 0;
}
