#include <iostream>
using namespace std;
long long n, i, a[100007], s, j, k, res;
int main() 
{
	cin>>n;
	for (i=1; i<=n; i++)
		cin>>a[i];
	j=n+1;
	for (i=n; i>=1; i--)
		{
		 if (a[i]<=k) break;
		 if (a[i]==a[j]) {res++; s+=res;} 
		 else res=0;
		 s+=a[i]-k; 
		 k++;
		 j--;
		}
	cout<<s;
	return 0;
}