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