#include<cstdio>
using namespace std;
int n,a[200005];
long long ans;
int main()
{
	scanf("%d",&n);for(int i=1;i<=n;++i) scanf("%d",&a[i]);a[0]=-1;
	for(int i=1;i<=n+1;++i) {
		if(a[i]==a[i-1]+1) continue;
		else if(a[i]>a[i-1]+1) {printf("-1\n");return 0;}
		else ans+=(long long)a[i-1];
	}
	printf("%lld\n",ans);
	return 0;
}