//Love and Freedom.
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long
#define inf 20021225
#define N 200010
using namespace std;

int a[N],n;

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