#include<bits/stdc++.h>

using namespace std;

long long n,ans,a[300000];

int main(){
	scanf("%lld",&n);
	for (int i=1;i<=n;++i) scanf("%lld",&a[i]);
	for (int i=n;i;--i){
		if (a[i]>=i||a[i+1]-1>a[i]){puts("-1"); return 0;}
		if (a[i]!=a[i+1]-1) ans+=a[i];
	}
	printf("%lld\n",ans);
	
	return 0;
}
			