#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, sum = 0;
  cin >> n;
  vector<int> tall(n);
  for (int i = 0; i < n; i++) {
    cin >> tall.at(i);
  }
  for (int i = 0; i < n - 1; i++) {
    int b;
    b = tall.at(i + 1) - tall.at(i);
    if (b >= 0) {
      continue;
    } else {
      tall.at(i + 1) = tall.at(i);
      sum += -b;
    }
  }
  cout << sum << endl;
}
