#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> row;
  int current;
  for (int i = 0; i < n; i++) {
    cin >> current;
    row.push_back(current);
  }
  int stoles = 0;
  for (int j = 1; j < n; j++) {
    int diff = row[j] - row[j - 1];
    if (diff < 0) {
      stoles -= diff;
      row[j] -= diff;
    }
  }
  cout << stoles;
  return 0;
}
