#include <bits/stdc++.h>
using namespace std;
int main(int argc, const char *argv[]) {
  int n;
  cin >> n;
  int sum = 0, pre, x;
  for (int i = 0; i < n; i++) {
    cin >> x;
    if (i > 0 && x < pre) {
      sum += pre - x;
      x = pre;
    }
    pre = x;
  }
  cout << sum << endl;
}
