#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, diff;
  long long counter = 0;
  cin >> n;
  int arr[n];
  cin >> arr[0];
  for (i = 1; i < n; i++) {
    cin >> arr[i];
    diff = arr[i - 1] - arr[i];
    if (diff > 0) counter += diff;
  }
  cout << counter;
  return 0;
}
