Buggy python submission (verdict: Time Limit Exceeded):
def solve(n, aaa):
    if aaa[0] > 0:
        return -1
    if any(a1 - a0 > 1 for a0, a1 in zip(aaa, aaa[1:])):
        return -1
    ans = 0
    aaa.reverse()

    while any(aaa):
        while aaa[-1] == 0:
            aaa.pop()
        aaa.reverse()
        while aaa[-1] == 0:
            aaa.pop()
        aaa.reverse()

        new = [0] * (len(aaa) - 1)
        a1 = aaa[0]
        ap = 0
        for i, a0 in enumerate(aaa[1:]):
            if a1 == 0:
                a1 = a0
                continue
            ans += 1
            if a1 - a0 < 1:
                new[i] = ap = max(a1 - 1, ap)
            else:
                new[i] = ap
            a1 = a0
            if ap:
                ap -= 1
        if a1:
            ans += 1
        aaa = new
    return ans


n = int(input())
aaa = list(map(int, (input() for _ in range(n))))
print(solve(n, aaa))


Fixed submission:
def solve(n, aaa):
    if aaa[0] > 0:
        return -1
    if any(a1 - a0 > 1 for a0, a1 in zip(aaa, aaa[1:])):
        return -1
    ans = 0
    a0 = 0

    for a1 in aaa[1:]:
        if a1 - a0 != 1:
            ans += a0
        a0 = a1

    return ans + a0


n = int(input())
aaa = list(map(int, (input() for _ in range(n))))
print(solve(n, aaa))

Buggy python submission (verdict: Time Limit Exceeded):
def solve(n, aaa):
    if aaa[0] > 0:
        return -1
    if any(a1 - a0 > 1 for a0, a1 in zip(aaa, aaa[1:])):
        return -1
    ans = 0
    aaa.reverse()

    while any(aaa):
        while aaa[-1] == 0:
            aaa.pop()
        aaa.reverse()
        while aaa[-1] == 0:
            aaa.pop()
        aaa.reverse()

        new = [0] * (len(aaa) - 1)
        a1 = aaa[0]
        ap = 0
        for i, a0 in enumerate(aaa[1:]):
            if a1 == 0:
                a1 = a0
                continue
            ans += 1
            if a1 - a0 < 1:
                new[i] = ap = max(a1 - 1, ap)
            else:
                new[i] = ap
            a1 = a0
            if ap:
                ap -= 1
        if a1:
            ans += 1
        aaa = new
    return ans


n = int(input())
aaa = list(map(int, (input() for _ in range(n))))
print(solve(n, aaa))


Fixed submission:
def solve(n, aaa):
    if aaa[0] > 0:
        return -1
    if any(a1 - a0 > 1 for a0, a1 in zip(aaa, aaa[1:])):
        return -1
    ans = 0
    a0 = 0

    for a1 in aaa[1:]:
        if a1 - a0 != 1:
            ans += a0
        a0 = a1

    return ans + a0


n = int(input())
aaa = list(map(int, (input() for _ in range(n))))
print(solve(n, aaa))

Buggy python submission (verdict: Runtime Error):
import numpy as np


def solve(n, aaa):
    if aaa[0] > 0:
        return -1
    if any(a1 - a0 > 1 for a0, a1 in zip(aaa, aaa[1:])):
        return -1
    ans = 0
    aaa.reverse()

    while any(aaa):
        new = [0] * (len(aaa) - 1)
        a1 = aaa[0]
        ap = 0
        for i, a0 in enumerate(aaa[1:]):
            if a1 == 0:
                a1 = a0
                continue
            ans += 1
            if a1 - a0 < 1:
                new[i] = ap = max(a1 - 1, ap)
            else:
                new[i] = ap
            a1 = a0
            if ap:
                ap -= 1
        aaa = np.trim_zeros(new)
        if a1:
            ans += 1
    return ans


n = int(input())
aaa = list(map(int, (input() for _ in range(n))))
print(solve(n, aaa))


Fixed submission:
def solve(n, aaa):
    if aaa[0] > 0:
        return -1
    if any(a1 - a0 > 1 for a0, a1 in zip(aaa, aaa[1:])):
        return -1
    ans = 0
    a0 = 0

    for a1 in aaa[1:]:
        if a1 - a0 != 1:
            ans += a0
        a0 = a1

    return ans + a0


n = int(input())
aaa = list(map(int, (input() for _ in range(n))))
print(solve(n, aaa))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np


def solve(n, aaa):
    if aaa[0] > 0:
        return -1
    if any(a1 - a0 > 1 for a0, a1 in zip(aaa, aaa[1:])):
        return -1
    ans = 0
    aaa.reverse()

    while any(aaa):
        new = [0] * (len(aaa) - 1)
        a1 = aaa[0]
        ap = 0
        for i, a0 in enumerate(aaa[1:]):
            if a1 == 0:
                a1 = a0
                continue
            ans += 1
            if a1 - a0 < 1:
                new[i] = ap = max(a1 - 1, ap)
            else:
                new[i] = ap
            a1 = a0
            if ap:
                ap -= 1
        aaa = np.trim_zeros(new)
        if a1:
            ans += 1
    return ans


n = int(input())
aaa = list(map(int, (input() for _ in range(n))))
print(solve(n, aaa))


Fixed submission:
def solve(n, aaa):
    if aaa[0] > 0:
        return -1
    if any(a1 - a0 > 1 for a0, a1 in zip(aaa, aaa[1:])):
        return -1
    ans = 0
    a0 = 0

    for a1 in aaa[1:]:
        if a1 - a0 != 1:
            ans += a0
        a0 = a1

    return ans + a0


n = int(input())
aaa = list(map(int, (input() for _ in range(n))))
print(solve(n, aaa))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np


def solve(n, aaa):
    if aaa[0] > 0:
        return -1
    if any(a1 - a0 > 1 for a0, a1 in zip(aaa, aaa[1:])):
        return -1
    ans = 0
    aaa.reverse()

    while any(aaa):
        new = [0] * (len(aaa) - 1)
        for i, (a1, a0) in enumerate(zip(aaa, aaa[1:])):
            if a1 == 0:
                continue
            ans += 1
            if a1 - a0 < 1:
                ap = a1
            else:
                ap = 0
            new[i] = max(ap - 1, new[i - 1] - 1, 0)
        aaa = np.trim_zeros(new) + [0]
    return ans


n = int(input())
aaa = list(map(int, (input() for _ in range(n))))
print(solve(n, aaa))


Fixed submission:
def solve(n, aaa):
    if aaa[0] > 0:
        return -1
    if any(a1 - a0 > 1 for a0, a1 in zip(aaa, aaa[1:])):
        return -1
    ans = 0
    a0 = 0

    for a1 in aaa[1:]:
        if a1 - a0 != 1:
            ans += a0
        a0 = a1

    return ans + a0


n = int(input())
aaa = list(map(int, (input() for _ in range(n))))
print(solve(n, aaa))