#include <bits/stdc++.h>
int main(void) {
  int N, i, count = 0;
  int A[N];
  scanf("%d", &N);
  for (i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }
  for (i = 0; i < N - 1; i++) {
    while (A[i] > A[i + 1]) {
      A[i + 1]++;
      count++;
    }
  }
  printf("%d", count);
  return 0;
}
