import std.stdio, std.conv, std.string, std.bigint;
import std.math, std.random, std.datetime;
import std.array, std.range, std.algorithm, std.container;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }

void main(){
	
	long n = read.to!long;
	long[] as;
	foreach(i; 0 .. n) as ~= read.to!long;
	
	long old_a = 0;
	long ans = 0;
	foreach_reverse(a; as){
		if(a >= old_a){
			ans += a;
			}
		else if(a == old_a - 1){
			ans += 0;
			}
		else{
			ans = -1;
			break;
			}
		debug writeln("a:", a, " old_a:", old_a, " ans:", ans);
		old_a = a;
		}
	if(old_a > 0) ans = -1;
	
	ans.writeln;
	}