program test
implicit none
integer(8) :: n,i
integer(8) :: retsu(2*10**5) = 0, imp=0, number=0

read(*,*) n

do i=1,n
	read(*,*) retsu(i)
enddo

if(retsu(1)>0) then
imp = 1
endif

do i=2,n

		if(retsu(i) - retsu(i-1) > 1) then
			imp = 1
		endif
		
		if(retsu(i)==0) then
			number = number
		else if(retsu(i)==1)then
			number = number + 1
		else if(retsu(i) - retsu(i-1) == 1) then
			number = number + 1
		else if(retsu(i) <= retsu(i-1)) then
			number = number + retsu(i)
		endif
			
enddo

if(imp == 1)then
	write(*,*) -1
else
	write(*,*) number
endif

end program