#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> x;
  for (int i = i; i < N; i++) {
    int A;
    cin >> A;
    x.push_back(A);
  }
  int total = 0;
  for (int i = 0; i < x.size() - 1; i++) {
    if (x[i] > x[i + 1]) {
      while (x[i] > x[i + 1]) {
        x[i + 1] = x[i + 1] + 1;
        total++;
      }
    }
  }
  if (x[x.size() - 1] < x[x.size() - 1]) {
    while (x[x.size() - 1] < x[x.size() - 1]) {
      x[x.size() - 1] = x[x.size() - 1] + 1;
      total++;
    }
  }
  cout << total << endl;
}
