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