#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	long i, n, k, g = 0, j = 0;
	long prc = 0;

	scanf("%ld", &n);
	long *a = malloc(n*sizeof(long));
	for (i = 0; i < n; i++) {
		scanf("%ld", &k);
		a[i] = k;
	}

	for (i = n-1; i >= 0; i--) {
		if (i != n-1 && a[i] != a[i+1]) {
			j += g;
			g = 0;
		}
		if (a[i]-j <= 0)
			break;

		prc += a[i]-j;
		g++;
	}

	printf("%ld\n", prc);

	free(a);

	return 0;
}
