#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 res = 0;
  for (int i = 1; i < N; i++) {
    int diff = A[i] - A[i - 1];
    if (diff < 0) {
      A[i] -= diff;
      res -= diff;
    }
  }
  cout << res;
  return 0;
}
