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