Problem p02972 (python)

Fixed submission:
def add(n):
    return n + 1

if __name__ == "__main__":
    n = int(input())
    buf = input().split()
    a = [int(s) for s in buf]
    
    boxes = [0] * n

    for i in reversed(range(n)):
        pick_boxes = boxes[i::i+1]
        pick_boxes.pop(0)
        if pick_boxes:
            if (sum(pick_boxes) % 2) == a[i]:
                boxes[i] = 0
            else:
                boxes[i] = 1
        else:
            boxes[i] = a[i]

    box_name = [i for i, x in enumerate(boxes) if x == 1]
    box_name = list(map(add, box_name))
    print(len(box_name))
    if len(box_name) != 0:
        box_name = ' '.join(map(str,box_name))
        print(box_name)

Buggy submission (verdict: Runtime Error):
import math
def add(n):
    return n + 1

def lcm(a):
    ans = a[0]
    for i in range(1, len(a)-1):
        ans = ans * a[i] // math.gcd(ans, a[i])
    return ans

if __name__ == "__main__":

    n = int(input())
    buf = input().split()
    a = [int(s) for s in buf]

    indexes = [i for i, x in enumerate(a) if x == 1]
    indexes = list(map(add, indexes))
    
    boxes = []
    if indexes:
        if max(indexes) > n:
            print("-1")
        else:
            
            while 1:
                num = lcm(indexes)
                if num < n:
                    break
                buf = [x for x in indexes if (max(indexes) % x) == 0]
                boxes.extend(buf)
                indexes = [x for x in indexes if (max(indexes) % x) != 0]
                if not indexes:
                    break 

            print("{}".format(len(boxes)+1))
            if boxes:
                boxes = ' '.join(map(str,boxes)) + " " + str(num)
            else:
                boxes = str(num)
            print(boxes)
    else:
        print("0")

Buggy submission (verdict: Runtime Error):
import math
def add(n):
    return n + 1

def lcm(a):
    ans = a[0]
    for i in range(1, len(a)):
        ans = ans * a[i] // math.gcd(ans, a[i])
    return ans

if __name__ == "__main__":

    n = int(input())
    buf = input().split()
    a = [int(s) for s in buf]

    indexes = [i for i, x in enumerate(a) if x == 1]
    indexes = list(map(add, indexes))
    
    boxes = []
    if indexes:
        num = lcm(indexes)
        if max(indexes) > n:
            print("-1")
        else:
            while num > n:
                boxes.append(indexes.pop())
                num = lcm(indexes)
            print("{}".format(len(boxes)+1))
            if boxes:
                boxes = ' '.join(map(str,boxes)) + " " + str(num)
            else:
                boxes = str(num)
            print(boxes)
    else:
        print("0")

Buggy submission (verdict: Runtime Error):
import math
def add(n):
    return n + 1

def lcm(a):
    ans = a[0]
    for i in range(1, len(a)):
        ans = ans * a[i] // math.gcd(ans, a[i])
    return ans

if __name__ == "__main__":

    n = int(input())
    buf = input().split()
    a = [int(s) for s in buf]

    indexes = [i for i, x in enumerate(a) if x == 1]
    indexes = list(map(add, indexes))
    
    boxes = []
    if indexes:
        num = lcm(indexes)
        if max(indexes) > n:
            print("-1")
        else:
            while num > n:
                boxes.append(indexes.pop())
                num = lcm(indexes)
            print("{}".format(len(boxes)+1))
            print(boxes)
            boxes = ' '.join(map(str,boxes)) + " " + str(num)
            print(boxes)
    else:
        print("0")

Buggy submission (verdict: Runtime Error):
import math
def add(n):
    return n + 1

def lcm(a):
    ans = a[0]
    for i in range(1, len(a)):
        ans = ans * a[i] // math.gcd(ans, a[i])
    return ans

if __name__ == "__main__":

    n = int(input())
    buf = input().split()
    a = [int(s) for s in buf]

    indexes = [i for i, x in enumerate(a) if x == 1]
    indexes = list(map(add, indexes))
    
    if indexes:
        num = lcm(indexes)
        if num > n:
            print("-1")
        else:
            print(num)
    else:
        print("0")