Problem p03464 (python)

Fixed submission:
k = int(input())
a = list(map(int, input().split()))
def solve():

    if a[k-1] != 2:
        print(-1)
        return

    mi = 2
    mx = 2

    for i in range(k):
        j = k-i-1

        mx = mx + a[j] - 1

        if j == 0:
            pass
        else:
            na = a[j-1]
            
            if mx % na != 0 and mi % na != 0 and ((mx % na) - (mi % na)) == (mx - mi):
                print(-1)
                return

            if mi%na != 0:
                mi = mi + (na - mi%na)

            if mx%na != 0:
                mx = mx - (mx % na)

    print(mi, mx)
solve()

Buggy submission (verdict: Runtime Error):
k = int(input())
a = list(map(int, input().split()))

def solve():

    if k == 1:
        if a[0] == 2:
            print(2, 3)
            return
        else:
            print(-1)
            return

    can = []

    for i in range(k):

        if i==0:
            can = [2]
        elif i > 1 and i != k-1:
            if a[k-i-1] == a[k-(i-1)-1]:
                continue

        oldcan = can[:]
        can = []

        if i == k-1:
            can = list(range(min(oldcan), max(oldcan) + a[k-i-1]))
        else:
            j = i+1
            mi = min(oldcan)
            mx = max(oldcan) + a[k-i-1] -1
            if mx < a[k-j-1]:
                print(-1)
                return
            #print(mi, mx)
            if mi%a[k-j-1] != 0:
                mi = mi + (a[k-j-1] - mi%a[k-j-1])
            if mx%a[k-j-1] != 0:
                mx = mx - mx % a[k-j-1]
            can = [mi, mx]

        # if len(can) == 0:
        #     print(-1)
        #     return
            #print(i, can, oldcan)
        # for c in oldcan:
        #     for a__ in range(c,c+a[k-i-1]):
        #         if i == k-1:
        #             if a__ not in can:
        #                 can.append(a__)
        #         else:
        #             if a__ not in can and a__ % a[k-(i+1)-1] == 0:
        #                 can.append(a__)

    print(min(can), max(can))
solve()

Buggy submission (verdict: Runtime Error):
k = int(input())
a = list(map(int, input().split()))

def solve():
    can = []
    res = []

    for i in range(k):
        a_ = a[k-i-1]

        if i==0:
            res = [2]
        else:
            res = []
            for c in can:
                if c % a_ == 0:
                    res.append(c)
            if len(res) == 0:
                print(-1)
                return

        can = []
        for r in res:
            can = can + list(range(r, r+a_))
            can = list(set(can))

        #print(i, res, a_, can)
    print(min(can), max(can))
solve()

Buggy submission (verdict: Runtime Error):
k = int(input())
a = list(map(int, input().split()))

can = []
res = []

def solve():

    for i in range(k):
        a_ = a[k-i-1]

        if i==0:
            res = [2]
        else:
            res = []
            for c in can:
                if c % a_ == 0:
                    res.append(c)
            if len(res) == 0:
                print(-1)
                return

        can = []
        for r in res:
            can = can + list(range(r, r+a[k-i-1]))

        #print(i, res, a_, can)
    print(min(can), max(can))
solve()