#include <cstdio>
using namespace std;
int v[200005];
int main() {
    int n;
    scanf("%d",&n);
    bool f=true;
    long long s=0;
    for(int i=0;i<n;i++)
        scanf("%d",&v[i]);
    for(int i=n;i--;)
    {
        if(v[i]>i||(i<n-1&&v[i]+1<v[i+1]))
            f=false;
        else if(i<n-1&&v[i]+1==v[i+1])continue;
        else s+=v[i];
    }
    if(f)
        printf("%lld\n",s);
    else
        printf("-1\n");
    return 0;
}
