#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 200010;
int n, a[maxn];
ll ans;
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &a[i]);
		if ((i == 1 && a[i]) || (i != 1 && a[i] > a[i - 1] + 1)) {
			printf("-1"); return 0;
		}
	}
	for (int i = 1; i <= n; ++i)
		if (i == n || a[i] + 1 != a[i + 1]) ans += a[i];
	printf("%lld", ans);
}