Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

a = []
for i in range(n):
    a.append(int(input()))

if a[0] > 0:
    print(-1)
    exit()

for i in range(1, n):
    if a[i] > i:
        print(-1)
        exit()
    if a[i] > a[i-1] + 1:
        print(-1)
        exit()

result = 0
last = n
b = [0 for i in range(n)]
while 1:
    for i in range(last):
        j = last - i - 1
        if a[j] != b[j]:
            start = j
            last = j
            max_diff = a[j]
            break
    if j == 0:
        print(result)
        exit()
    for i in range(max_diff):
        b[start - i] = max_diff - i
    result += max_diff

Fixed submission:
n = int(input())

a = []
for i in range(n):
    a.append(int(input()))

if a[0] > 0:
    print(-1)
    exit()

for i in range(1, n):
    if a[i] > i:
        print(-1)
        exit()
    if a[i] > a[i - 1] + 1:
        print(-1)
        exit()

if n == 1:
    print(0)
    exit()

result = 0

if a[n-1] != 0:
    result += a[n-1]

for i in range(2, n):
    j = n - i
    if a[j] != 0 and a[j] != a[j + 1] - 1:
            result += a[j]

print(result)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

a = []
for i in range(n):
    a.append(int(input()))

if a[0] > 0:
    print(-1)
    exit()

for i in range(1, n):
    if a[i] > i:
        print(-1)
        exit()
    if a[i] > a[i-1] + 1:
        print(-1)
        exit()

result = 0
last = n
b = [0 for i in range(n)]
while a != b:
    for i in range(last):
        j = last - i - 1
        if a[j] != b[j]:
            start = j
            last = j
            max_diff = a[j]
            break
    for i in range(max_diff):
        b[start - i] = max_diff - i
    result += max_diff

print(result)

Fixed submission:
n = int(input())

a = []
for i in range(n):
    a.append(int(input()))

if a[0] > 0:
    print(-1)
    exit()

for i in range(1, n):
    if a[i] > i:
        print(-1)
        exit()
    if a[i] > a[i - 1] + 1:
        print(-1)
        exit()

if n == 1:
    print(0)
    exit()

result = 0

if a[n-1] != 0:
    result += a[n-1]

for i in range(2, n):
    j = n - i
    if a[j] != 0 and a[j] != a[j + 1] - 1:
            result += a[j]

print(result)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

a = []
for i in range(n):
    a.append(int(input()))

if a[0] > 0:
    print(-1)
    exit()

for i in range(1, n):
    if a[i] > i:
        print(-1)
        exit()
    if a[i] > a[i-1] + 1:
        print(-1)
        exit()

result = 0
b = [0 for i in range(n)]
while a != b:
    max_diff = 0
    for i in range(n):
        j = n - i - 1
        if a[j] != b[j]:
            if a[j] > max_diff:
                start = j
                max_diff = a[j]
    for i in range(max_diff):
        b[start - i] = max_diff - i
    result += max_diff

print(result)

Fixed submission:
n = int(input())

a = []
for i in range(n):
    a.append(int(input()))

if a[0] > 0:
    print(-1)
    exit()

for i in range(1, n):
    if a[i] > i:
        print(-1)
        exit()
    if a[i] > a[i - 1] + 1:
        print(-1)
        exit()

if n == 1:
    print(0)
    exit()

result = 0

if a[n-1] != 0:
    result += a[n-1]

for i in range(2, n):
    j = n - i
    if a[j] != 0 and a[j] != a[j + 1] - 1:
            result += a[j]

print(result)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

a = []
for i in range(n):
    a.append(int(input()))

if a[0] > 0:
    print(-1)
    exit()

for i in range(1, n):
    if a[i] > i:
        print(-1)
        exit()
    if a[i] > a[i-1] + 1:
        print(-1)
        exit()

result = 0
b = [0 for i in range(n)]
while a != b:
    max_diff = 0
    for i in range(n):
        if a[i] != b[i]:
            if a[i] > max_diff:
                start = i
                max_diff = a[i]
    for i in range(max_diff):
        if b[start - i] != a[start - i]:
            b[start - i] = max_diff - i
    result += max_diff

print(result)


Fixed submission:
n = int(input())

a = []
for i in range(n):
    a.append(int(input()))

if a[0] > 0:
    print(-1)
    exit()

for i in range(1, n):
    if a[i] > i:
        print(-1)
        exit()
    if a[i] > a[i - 1] + 1:
        print(-1)
        exit()

if n == 1:
    print(0)
    exit()

result = 0

if a[n-1] != 0:
    result += a[n-1]

for i in range(2, n):
    j = n - i
    if a[j] != 0 and a[j] != a[j + 1] - 1:
            result += a[j]

print(result)