#include <iostream>
using namespace std;
typedef long long ll;

int main()
{
    int n;
    cin >> n;
    ll a[200003] {0};
    a[0] = -1;
    for(int i = 1; i <= n; i++) cin >> a[i];
    ll ans = 0;
    for(int i = 0; i <= n; i++){
        if(a[i] + 1 < a[i + 1]){
            cout << -1 << endl;
            return 0;
        }
        while(a[i] + 1 == a[i + 1]) i++;
        ans += a[i];
    }
    cout << ans << endl;
}