#include <bits/stdc++.h>
using namespace std;

typedef long long  LL;
int n,a[200010];
LL ans;

void work()
{
	scanf("%d",&n);
	for (int i=1; i<=n; i++)  scanf("%d",&a[i]);
	for (int i=n; i; i--)
		if (a[i]>=i)  puts("-1"),exit(0);
		else  if (a[i+1]-a[i]>1)  puts("-1"),exit(0);
		else  if (a[i]!=a[i+1]-1)  ans+=a[i];
	printf("%lld",ans);
}

int main()
{
	work();
	return 0;
}
