N = int( eval(input()))

A = [ int( eval(input())) for _ in range(N)]

now = 0

ans = 0

if A[0] != 0:

    ans = -1

else:

   for i in range(1,N):

       a = A[i]

       # if a > i:

       #     ans = -1

       #     break

       #el

       if now + 1 == a:

           ans += 1

           now += 1

       elif now >= a:

           ans += a

           now = a

       else:

           ans = -1

           break

print(ans)

import sys

input = sys.stdin.readline

def main():

    N = int( eval(input()))

    A = [ int( eval(input())) for _ in range(N)]

    if A[0] > 0:

        print((-1))

        return

    ans = 0

    for i in range(1,N):

        # print(i, A[i])

        if A[i] == 0:

            continue

        if A[i] == 1:

            ans += 1

            continue

        if A[i] == A[i-1]+1:

            ans += 1

            continue

        if A[i] <= A[i-1]:

            ans += A[i]

            continue

        print((-1))

        return

    print(ans)

if __name__ == '__main__':

    main()

N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
now = 0
ans = 0
if A[0] != 0:
    ans = -1
else:
    for i in range(1, N):
        a = A[i]
        # if a > i:
        #     ans = -1
        #     break
        # el
        if now + 1 == a:
            ans += 1
            now += 1
        elif now >= a:
            ans += a
            now = a
        else:
            ans = -1
            break
print(ans)

import sys

input = sys.stdin.readline


def main():
    N = int(eval(input()))
    A = [int(eval(input())) for _ in range(N)]
    if A[0] > 0:
        print((-1))
        return
    ans = 0
    for i in range(1, N):
        # print(i, A[i])
        if A[i] == 0:
            continue
        if A[i] == 1:
            ans += 1
            continue
        if A[i] == A[i - 1] + 1:
            ans += 1
            continue
        if A[i] <= A[i - 1]:
            ans += A[i]
            continue
        print((-1))
        return
    print(ans)


if __name__ == "__main__":
    main()

-N = int(eval(input())) -A = [int(eval(input())) for _ in range(N)] -now = 0 -ans = 0 -if A[0] != 0: -    ans = -1 -else: +import sys + +input = sys.stdin.readline + + +def main(): +    N = int(eval(input())) +    A = [int(eval(input())) for _ in range(N)] +    if A[0] > 0: +        print((-1)) +        return +    ans = 0 -        a = A[i] -        # if a > i: -        #     ans = -1 -        #     break -        # el -        if now + 1 == a: +        # print(i, A[i]) +        if A[i] == 0: +            continue +        if A[i] == 1: -            now += 1 -        elif now >= a: -            ans += a -            now = a -        else: -            ans = -1 -            break -print(ans) +            continue +        if A[i] == A[i - 1] + 1: +            ans += 1 +            continue +        if A[i] <= A[i - 1]: +            ans += A[i] +            continue +        print((-1)) +        return +    print(ans) + + +if __name__ == "__main__": +    main()