N = int(eval(input()))

A = [[]]



for _ in range(N):

    a = int(eval(input()))

    if len(A[-1]) > 0 and a < A[-1][-1]:

        A.append([])

    A[-1].append(a)



def isOk():

    for l, r in zip(A, A[1:]):

        if l[-1] + 1 < r[0]:

            return False

    for grp in A:

        for l, r in zip(grp, grp[1:]):

            if l + 1 < r:

                return False



    return A[0][0] == 0



if not isOk():

    print((-1))

    exit()



B = []

for a in A:

    B.extend(a)

B = [-10**18] + B[::-1]



ans = 0

for r, l in zip(B, B[1:]):

    if l >= r:

        ans += l

print(ans)

import sys

input = sys.stdin.buffer.readline



N = int(eval(input()))

G = [[]]

A = []



for _ in range(N):

    a = int(eval(input()))

    if len(G[-1]) > 0 and G[-1][-1] > a:

        G.append([])

    A.append(a)

    G[-1].append(a)



def isOk():

    if any(l[-1] + 1 < r[0] for l, r in zip(G, G[1:])):

        return False

    for grp in G:

        if any(l + 1 < r for l, r in zip(grp, grp[1:])):

            return False

    return A[0] == 0



if not isOk():

    print((-1))

    exit()



ans = 0

A = [-10**18] + A[::-1]



for r, l in zip(A, A[1:]):

    if l >= r:

        ans += l

print(ans)

N = int(eval(input()))
A = [[]]
for _ in range(N):
    a = int(eval(input()))
    if len(A[-1]) > 0 and a < A[-1][-1]:
        A.append([])
    A[-1].append(a)


def isOk():
    for l, r in zip(A, A[1:]):
        if l[-1] + 1 < r[0]:
            return False
    for grp in A:
        for l, r in zip(grp, grp[1:]):
            if l + 1 < r:
                return False
    return A[0][0] == 0


if not isOk():
    print((-1))
    exit()
B = []
for a in A:
    B.extend(a)
B = [-(10**18)] + B[::-1]
ans = 0
for r, l in zip(B, B[1:]):
    if l >= r:
        ans += l
print(ans)

import sys

input = sys.stdin.buffer.readline
N = int(eval(input()))
G = [[]]
A = []
for _ in range(N):
    a = int(eval(input()))
    if len(G[-1]) > 0 and G[-1][-1] > a:
        G.append([])
    A.append(a)
    G[-1].append(a)


def isOk():
    if any(l[-1] + 1 < r[0] for l, r in zip(G, G[1:])):
        return False
    for grp in G:
        if any(l + 1 < r for l, r in zip(grp, grp[1:])):
            return False
    return A[0] == 0


if not isOk():
    print((-1))
    exit()
ans = 0
A = [-(10**18)] + A[::-1]
for r, l in zip(A, A[1:]):
    if l >= r:
        ans += l
print(ans)

+import sys + +input = sys.stdin.buffer.readline -A = [[]] +G = [[]] +A = [] -    if len(A[-1]) > 0 and a < A[-1][-1]: -        A.append([]) -    A[-1].append(a) +    if len(G[-1]) > 0 and G[-1][-1] > a: +        G.append([]) +    A.append(a) +    G[-1].append(a) -    for l, r in zip(A, A[1:]): -        if l[-1] + 1 < r[0]: +    if any(l[-1] + 1 < r[0] for l, r in zip(G, G[1:])): +        return False +    for grp in G: +        if any(l + 1 < r for l, r in zip(grp, grp[1:])): -    for grp in A: -        for l, r in zip(grp, grp[1:]): -            if l + 1 < r: -                return False -    return A[0][0] == 0 +    return A[0] == 0 -B = [] -for a in A: -    B.extend(a) -B = [-(10**18)] + B[::-1] -for r, l in zip(B, B[1:]): +A = [-(10**18)] + A[::-1] +for r, l in zip(A, A[1:]):