#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 <iostream>
using namespace std;

int main(void) {
    long a, n, min, max;
    long sum = 0;
    
    cin >> n;
    
    cin >> a;
    max = a;
    min = a;
    sum += a;
    for(int i = 1; i < n; ++i) {
        cin >> a;
        if(a < min) {
            min = a;
        }
        if(a > max) {
            max = a;
        }
        sum += a;
    }
    
    cout << min << ' ' << max << ' ' <<  sum << endl;
    
    return 0;
}

