#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <iomanip>
#include <cassert>
#include <stack>
#include <queue>
#include <deque>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
//using namespace __gnu_pbds;

typedef long long ll;
typedef pair <int, int> pii;

// template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//  order_of_key (k) : Number of items strictly smaller than k .
//  find_by_order(k) : K-th element in a set (counting from zero).
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define pb push_back
#define ppb pop_back
#define mkp make_pair
#define F first
#define S second
#define show(a) cerr << #a <<" -> "<< a <<"\n"
#define fo(a, b, c, d) for(int (a) = (b); (a) <= (c); (a) += (d))
#define foo(a, b, c ,d) for(int (a) = (b); (a) >= (c); (a) -= (d))
//#define int ll

const int N = 2e5 + 5;
const int oo = 1e9 + 5;

int a[N];

void solve() {
	int n;
	cin >> n;
	bool sorry = 0;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
		if (a[i] > i) sorry = 1;
	}	
	if (sorry) {
		cout << -1;
		return;
	}
	ll ans = 0;
	for (int i = n-1; i > 0; --i) {
		if (a[i] <= a[i-1]) {
			ans += a[i];
		}
		else if (a[i] - a[i-1] == 1) {
			ans += a[i];
			while (i-1 >= 0 && a[i] - a[i-1] == 1) {
				--i;
			}
			if (i-1 >= 0 && a[i] > a[i-1]) {
				cout << -1;
				return;
			}
			//++i;
		}
		else {
			cout << -1;
			return;
		}
	}
	cout << ans;
}

 main () {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int tt = 1;
	while (tt --) {
		solve();
	}
	return 0;
}
/*
	“I’m not gonna run away, I never go back on my word! That’s my nindo: my ninja way.” Naruto

__¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
_¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
¶¶¶¶¶¶¶¶¶¶¶¶¶_¶¶_¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
¶¶¶¶¶¶¶¶¶¶¶¶¶_¶¶__¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
¶¶¶¶¶¶¶¶¶¶¶¶¶_¶¶__¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
¶¶¶¶¶¶¶¶¶¶¶¶¶__¶¶¶¶_¶¶¶_¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶______¶
¶¶¶¶¶¶¶¶¶¶¶¶¶__¶¶__¶¶¶¶¶_¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶_¶______
¶¶¶¶¶¶¶¶¶¶¶¶¶¶__¶¶¶¶¶_¶¶__¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶____¶¶_¶
¶¶¶¶¶¶¶¶¶¶¶¶¶¶_¶¶_¶_¶___¶__¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶_____¶_¶
¶¶¶¶¶¶¶¶¶¶¶¶¶¶__¶¶¶¶____¶¶_¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶__¶__¶_¶
¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶__________¶__¶¶¶¶¶¶¶¶¶¶¶¶¶¶_____¶_¶
¶_¶¶¶¶¶¶¶__¶¶¶¶______________¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶____¶¶
__¶¶¶¶¶¶¶___¶¶¶_______________¶¶¶¶¶¶¶¶¶¶¶¶¶¶_¶_¶¶¶
__¶¶¶¶¶¶____¶¶¶_______________¶¶¶¶¶¶¶¶¶¶¶¶__¶_¶¶¶¶
__¶¶¶¶¶______¶¶________________¶¶¶¶¶¶¶¶¶¶¶_¶__¶¶¶¶
__¶¶¶¶_________________________¶¶¶¶¶¶¶¶¶¶¶___¶¶¶¶¶
__¶¶¶_____¶¶__________¶¶¶______¶¶_¶¶¶¶¶¶¶¶_¶¶¶¶¶¶¶
__¶¶¶¶¶¶¶_¶¶________¶¶¶¶_______¶¶_¶¶¶¶¶¶¶¶_¶¶¶¶¶¶¶
___¶¶¶¶¶¶¶______¶¶¶¶¶¶__________¶_¶¶¶¶¶¶¶___¶¶¶¶¶¶
___¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶______________¶¶¶_¶¶___¶¶¶¶¶¶
___¶¶¶¶¶¶¶¶¶¶¶¶¶_¶¶¶_______________¶¶¶_¶¶___¶¶¶¶¶¶
____¶¶¶¶¶¶¶¶¶¶____¶¶_______________¶¶__¶____¶_¶¶¶¶
_____¶¶¶¶¶¶¶¶¶__¶¶¶________________¶¶_¶_______¶¶¶¶
______¶¶¶¶¶¶¶¶¶¶___________________¶¶_________¶¶¶¶
_______¶¶¶¶¶¶¶¶¶______¶¶¶__________¶¶__________¶¶¶
________¶¶¶¶¶¶¶¶____¶¶¶¶___________¶_____________¶
________¶¶¶¶¶¶¶___¶¶___¶¶¶________¶¶_____________¶
__________¶¶¶__¶¶¶¶______¶¶______________________¶
___________¶¶_____________¶¶¶_________¶___________
___________¶¶_______________¶________¶¶___________
_____________________________¶_______¶____________

_______________________¶_________¶¶
________________¶_____¶_¶_______¶_¶
________¶______¶¶¶¶___¶__¶____¶¶__¶________¶¶
________¶¶¶¶¶__¶¶_¶__¶____¶_¶¶¶___¶¶____¶¶¶¶¶
________¶¶__¶¶¶¶___¶¶¶_____¶¶_____¶¶_¶¶¶¶__¶¶
_________¶_____¶____¶______________¶¶¶_____¶
__________¶________________________________¶
__¶¶¶_¶¶__¶¶______________________________¶¶
__¶¶¶__¶__________________________________¶¶¶¶
____¶¶_______________________________________¶¶¶¶¶
_____¶¶_________________________________________¶¶¶
_______¶¶________________________________¶¶__¶¶¶
___¶¶¶¶¶¶___________________________¶____¶¶¶¶
_¶¶¶_______________________¶¶¶¶¶___¶¶___¶¶¶¶¶
_¶¶___________________¶¶¶¶¶¶¶¶¶¶__¶¶¶___¶¶¶_¶¶
___¶¶¶____________¶¶¶¶¶¶¶¶¶¶_¶¶¶_¶¶¶¶¶_¶¶¶__¶¶¶
____¶¶______¶__¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶_¶¶¶¶_¶¶¶
__¶¶_______¶¶_¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶_¶¶¶__¶
_¶¶______¶¶¶_¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
_¶¶¶¶¶¶¶¶¶¶¶¶¶¶_¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
____¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
____¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶__¶_________¶¶¶¶¶¶¶
______¶¶¶¶¶¶¶¶_____¶¶¶¶_____¶________¶¶¶¶__¶
______¶¶¶¶________¶¶__¶¶___¶¶_________¶¶¶__¶
_______¶¶¶________¶_¶__¶¶__¶___________¶___¶
_______¶¶¶________¶_¶__¶¶_¶¶___¶¶¶¶¶¶_______¶
______¶¶¶¶________¶¶_¶¶¶¶¶¶_¶¶¶¶¶¶___________¶
______¶¶¶¶_________¶¶_¶__¶¶_______¶¶¶_________¶
______¶¶¶¶__________¶¶________¶¶¶¶_________¶¶¶
_____¶¶¶¶¶_________¶__¶¶¶_____¶___¶¶_______¶¶
_____¶¶¶¶¶¶______¶¶¶____¶¶_____¶¶¶_____¶¶¶¶¶
_____¶¶¶¶¶¶___¶¶¶¶¶¶_____¶¶___¶¶__________¶
____¶¶¶¶¶_¶__¶__¶_________¶¶_____________¶
____¶¶¶¶¶_¶¶¶¶¶¶¶¶¶¶¶_____________________¶
____¶¶¶¶¶_¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶_____________¶¶
___¶¶¶¶¶¶_¶¶_¶¶_¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶_¶¶¶¶¶¶
___¶¶¶¶¶¶_¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
___¶¶¶¶¶__¶¶¶¶¶_¶¶_¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
__¶¶¶_¶¶_¶¶¶¶¶¶¶¶¶__¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
__¶¶¶¶¶¶_¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
__¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
_¶¶¶¶¶¶¶¶¶_¶¶¶¶¶¶¶¶¶¶¶¶¶¶_¶¶¶¶¶¶¶¶¶¶
_¶¶¶¶¶¶¶¶_¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶_¶¶¶¶¶¶
¶¶¶¶¶¶¶¶_¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
___¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶
*/

Measured memory_bytes: 1024