#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int N, ans = 0, max = 0;
  cin >> N;
  int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N; i++) {
    while (1) {
      if (A[i] < max) {
        A[i]++;
        ans++;
      } else {
        break;
      }
    }
    max = A[i];
  }
  cout << ans << endl;
}
