#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)

int n,a[200010];
signed main(){
	cin>>n;
	rep(i,n)cin>>a[i];
	if(a[0]){
		cout<<-1<<endl;
		return 0;
	}
	int now=a[0];
	int ans=0;
	for(int i=1;i<n;i++){
		if(now+2<=a[i]){
			cout<<-1<<endl;
			return 0;
		}
		if(now>=a[i])ans+=now;
		now=a[i];
	}
	ans+=now;
	cout<<ans<<endl;
	return 0;
}
