#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int main() {
  long long n, i, c = 0;
  cin >> n;
  long long a[n];
  for (int j = 0; j < n; j++) cin >> a[i];
  for (i = 0; i < n; i++) {
    while (a[i] > a[i + 1]) {
      a[i + 1]++;
      c++;
    }
  }
  cout << c << endl;
}
