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