#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> people(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> people[i];
  }
  int chair_length = 0;
  int taller = people[0];
  for (int i = 0; i < n; i++) {
    int x = people[i];
    taller = max(taller, people[i]);
    if (x < taller) {
      chair_length += taller - x;
      people[i] = taller;
    }
  }
  cout << chair_length;
}
