#include <iostream>
using namespace std;

int main() {
	int N;
	cin >> N;
	long long ans = 0, before;
	cin >> before;
	bool HasNoAns = (before > 0);
	for (int i = 1; i < N; i++) {
		int now;
		cin >> now;
		if (now == 0) {
			before = now;
			continue;
		}
		if (now == before + 1) ans++;
		else {
			if (now <= before) ans += now;
			else HasNoAns = true;
		}
		before = now;
	}
	cout << (HasNoAns ? -1 : ans) << endl;
}
#include <bits/stdc++.h>
using namespace std;
int a[200002], b[200002];
int main(){
    int n;
    scanf("%d", &n);
    bool ok = 1;
    for(int i=1;i<=n;i++){
        scanf("%d", &a[i]);
        if(a[i] > a[i-1] && a[i]-a[i-1] > 1){
            ok = 0;
        }
        if(i == 1 && a[i] != 0){
            ok = 0;
        }
    }   
    if(!ok) printf("-1\n");
    else{
        long long ans = 0;
        for(int i=n;i>=1;i--){
            if(a[i] == 0) continue;
            if(a[i]+1 == a[i+1]) continue;
            ans += a[i];
        }
        printf("%lld\n", ans);
    }
}