#include <iostream>
using namespace std;

const int MAXN = 2e5 + 10;
long long n, a[MAXN], mx, ans;

int main() {
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	if (a[0] > 0)
		return cout << -1, 0;
	mx = -1;
	for (int i = n - 1; i >= 0; i--, mx--) {
		if (mx < a[i])
			ans += a[i], mx = a[i];
		else if (mx > a[i])
			return cout << -1, 0;
	}
	cout << ans;
}