#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=200010;
int n,a[N],b[N];
ll ans;
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&a[i]);
		if(a[i]>i){printf("-1");return 0;}
		b[i]=i-a[i];
	}
	for(int i=0;i<n-1;i++) if(b[i]>b[i+1]){printf("-1");return 0;}
	for(int i=0;i<n;i++){
		while(b[i]==b[i+1]&&i<n-1) i++;
		ans+=a[i];
	}
	printf("%lld",ans);
	return 0;
}