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