#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

vector<long long> a; long long last = 0, potratili = 0;
long long pred, minuss = 0, pred_minuss = 0;

void pristrii() {
	if (pred == a.back()) {
		potratili += (pred - pred_minuss);
		minuss += 1;
		a.pop_back();
	}
	else {
		pred = a.back();
		potratili += (pred - minuss);
		pred_minuss = minuss;
		minuss += 1;
		a.pop_back();
	}
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	long long n;
	cin >> n;
	a.resize(n);
	for (long long i = 0; i < n; i++) {
		cin >> a[i];
	}
	while (a.back() - minuss > 0) {
		if (a.size() == 0) {
			break;
		}
		pristrii();
	}
	cout << potratili << '\n';
}