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

const int N = 2e5 + 10;

int a[N];

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