n = int(eval(input()))

A = [-1]

for _ in range(n):

    A.append(int(eval(input())))



num = 0

count = 0

if A[1] != 0:

    print((-1))

else:

    for i in range(n,0,-1):

        if A[i] > num:

            num = A[i]

            count += num

        elif A[i] < num:

            print((-1))

            break

        num -= 1

    else:

        print(count)

    
n = int(eval(input()))

list = [int(eval(input())) for _ in range(n)]



def main(n, list):

    result = 0

    num = 0

    if list[0] != 0:

        return -1

    

    for i in range(n - 1, - 1, - 1):

        if list[i] >= num:

            result += list[i]

            num = list[i]

        elif list[i] == num - 1:

            num -= 1

        else:

            return -1

    return result



print((main(n, list)))
n = int(eval(input()))
A = [-1]
for _ in range(n):
    A.append(int(eval(input())))
num = 0
count = 0
if A[1] != 0:
    print((-1))
else:
    for i in range(n, 0, -1):
        if A[i] > num:
            num = A[i]
            count += num
        elif A[i] < num:
            print((-1))
            break
        num -= 1
    else:
        print(count)

n = int(eval(input()))
list = [int(eval(input())) for _ in range(n)]


def main(n, list):
    result = 0
    num = 0
    if list[0] != 0:
        return -1
    for i in range(n - 1, -1, -1):
        if list[i] >= num:
            result += list[i]
            num = list[i]
        elif list[i] == num - 1:
            num -= 1
        else:
            return -1
    return result


print((main(n, list)))

-A = [-1] -for _ in range(n): -    A.append(int(eval(input()))) -num = 0 -count = 0 -if A[1] != 0: -    print((-1)) -else: -    for i in range(n, 0, -1): -        if A[i] > num: -            num = A[i] -            count += num -        elif A[i] < num: -            print((-1)) -            break -        num -= 1 -    else: -        print(count) +list = [int(eval(input())) for _ in range(n)] + + +def main(n, list): +    result = 0 +    num = 0 +    if list[0] != 0: +        return -1 +    for i in range(n - 1, -1, -1): +        if list[i] >= num: +            result += list[i] +            num = list[i] +        elif list[i] == num - 1: +            num -= 1 +        else: +            return -1 +    return result + + +print((main(n, list)))