#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2 * 10e5;
int n, a[MAXN], ans = 0;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i < n; i++) {
    while (a[i + 1] < a[i]) {
      ans++;
      a[i + 1]++;
    }
  }
  cout << ans;
  return 0;
}
