#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int length, sum = 0;
  cin >> length;
  int ppl[length];
  for (int i = 0; i < length; i++) cin >> ppl[i];
  for (int i = 1; i < length; i++) {
    if (ppl[i - 1] + sum > ppl[i]) sum += ppl[i - 1] + sum - ppl[i];
  }
  cout << sum << endl;
  return 0;
}
