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