#include <iostream>
#include <algorithm>
#include <stack>

using namespace std;

stack<int> a; int last = 0, potratili = 0;
int pred, minuss = 0, pred_minuss = 0;

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

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	int n, in;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> in;
		a.push(in);
	}
	while (a.top() - minuss > 0) {
		if (a.size() == 0) {
			break;
		}
		pristrii();
	}
	cout << potratili << '\n';
}