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