#include <cstdio>
#include <algorithm>

const int N = 100000;
int n, a[N];

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) scanf("%d", a + i);
	int d = 0;
	long long ans = 0;
	for (int i = n - 1; i >= 0;) {
		int amt = 0;
		do {
			++amt;
			ans += std::max(0, a[i] - d);
			--i;
		} while (i >= 0 && a[i] == a[i + 1]);
		d += amt;
	}
	printf("%lld\n", ans);
	return 0;
}