Problem p02585 (python)

Fixed submission:
from itertools import accumulate


def main():
    cells, moves = [int(x) for x in input().split()]
    jumps_from = [int(x) - 1 for x in input().split()]
    score_on = [int(x) for x in input().split()]

    ans = - 10**10
    for start in range(cells):
        route = []
        route_append = route.append
        position = start
        while True:
            route_append(score_on[position])
            position = jumps_from[position]
            if position == start:  # Looped
                break

        length = len(route)
        total = sum(route)
        accum_tmp = 0
        for i, score in enumerate(route):
            accum_tmp += score
            if i + 1 > moves:
                break
            accum = accum_tmp
            if total > 0:
                accum += total * ((moves - (i + 1)) // length)
            ans = max(ans, accum)

        # if total > 0:
        #     q, mod = divmod(moves, length)
        #     if mod == 0:
        #         candidate = total * q
        #     else:
        #         candidate = max(list(accumulate(route))[:mod]) + total * q
        # else:
        #     candidate = max(list(accumulate(route))[:min(moves, length)])
        # ans = max(ans, candidate)
    return ans


if __name__ == '__main__':
    ans = main()
    print(ans)

Buggy submission (verdict: Runtime Error):
from itertools import accumulate


def main():
    cells, moves = [int(x) for x in input().split()]
    nexts = [int(x) - 1 for x in input().split()]
    scores = [int(x) for x in input().split()]

    ans = - 10**20
    for start in range(cells):
        route = []
        route_append = route.append
        now = start
        for _step in range(cells):
            nex = nexts[now]
            route_append(scores[nex])
            if nex == start:
                break
            now = nex

        length = len(route)
        total = sum(route)
        if total > 0:
            q, mod = divmod(moves, length)
            candidate = max(list(accumulate(route))[:mod]) + total * q
        else:
            candidate = max(list(accumulate(route))[:min(moves, length)])
        ans = max(ans, candidate)

    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from itertools import accumulate


def main():
    cells, moves = [int(x) for x in input().split()]
    nexts = [int(x) - 1 for x in input().split()]
    scores = [int(x) for x in input().split()]

    ans = -10 ** 9 - 5
    # whole_break = False
    for start in range(cells):
        route = []
        seen = set()
        route_append = route.append
        seen_add = seen.add
        now = start
        for _step in range(cells):
            nex = nexts[now]
            # if nex < start:
            #     whole_break = True
            #     break
            if nex in seen:
                break
            route_append(scores[nex])
            seen_add(nex)
            now = nex

        length = len(route)
        total = sum(route)
        # if whole_break:
        #     whole_break = False
        #     if moves > length:
        #         break

        if total > 0 and moves > length:
            q, mod = divmod(moves, length)
            candidate = max(list(accumulate(route))[:mod]) + total * q
        else:
            candidate = max(list(accumulate(route))[:min(moves, length)])
        ans = max(ans, candidate)

    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    cells, moves = [int(x) for x in input().split()]
    nexts = [int(x) - 1 for x in input().split()]
    scores = [int(x) for x in input().split()]

    ans = -10 ** 9 - 5
    # whole_break = False
    for start in range(cells):
        route = []
        seen = set()
        route_append = route.append
        seen_add = seen.add
        now = start
        for _step in range(cells):
            nex = nexts[now]
            # if nex < start:
            #     whole_break = True
            #     break
            if nex in seen:
                break
            route_append(scores[nex])
            seen_add(nex)
            now = nex

        length = len(route)
        total = sum(route)
        # if whole_break:
        #     whole_break = False
        #     if moves > length:
        #         break

        if total > 0 and moves > length:
            q, mod = divmod(moves, length)
            candidate = max(sum(route[:x + 1]) for x in range(mod)) + total * q
        else:
            candidate = max(sum(route[:x + 1]) for x in range(length))
        ans = max(ans, candidate)

    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    cells, moves = [int(x) for x in input().split()]
    nexts = [int(x) - 1 for x in input().split()]
    scores = [int(x) for x in input().split()]
    ans = - 10 ** 9 - 5
    for start in range(cells):
        route = []
        seen = [False] * cells
        now = start
        for _step in range(cells):
            nex = nexts[now]
            if seen[nex]:
                break
            route.append(scores[nex])
            now = nex
            seen[now] = True
        length = len(route)
        total = sum(route)
        if total > 0 and moves > length:
            q, mod = divmod(moves, length)
            candidate = max(sum(route[:x + 1]) for x in range(mod)) + total * q
        else:
            candidate = max(sum(route[:x + 1])
                            for x in range(max(min(moves, length), 1)))
        ans = max(ans, candidate)

    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    cells, moves = [int(x) for x in input().split()]
    nexts = [int(x) - 1 for x in input().split()]
    scores = [int(x) for x in input().split()]
    ans = - 10 ** 9 - 5
    for start in range(cells):
        route = []
        seen = [False] * cells
        now = start
        for _step in range(cells):
            nex = nexts[now]
            if seen[nex]:
                break
            route.append(scores[nex])
            now = nex
            seen[now] = True
        length = len(route)
        total = sum(route)
        if total > 0 and moves > length:
            q, mod = divmod(moves, length)
            candidate = max(sum(route[:x + 1]) for x in range(mod)) + total * q
        else:
            candidate = max(sum(route[:x + 1])
                            for x in range(max(min(moves, length), 1)))
        ans = max(ans, candidate)

    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    cells, moves = [int(x) for x in input().split()]
    nexts = [int(x) - 1 for x in input().split()]
    scores = [int(x) for x in input().split()]
    ans = - 10 ** 9 - 5
    for start in range(cells):
        route = []
        seen = [False] * cells
        now = start
        for _step in range(cells):
            nex = nexts[now]
            if seen[nex]:
                break
            route.append(scores[nex])
            now = nex
            seen[now] = True
        print(route)
        length = len(route)
        total = sum(route)
        if total > 0 and moves > length:
            q, mod = divmod(moves, length)
            candidate = max(sum(route[:x + 1]) for x in range(mod)) + total * q
        else:
            candidate = max(sum(route[:x + 1])
                            for x in range(max(min(moves, length), 1)))
        ans = max(ans, candidate)

    print(ans)


if __name__ == '__main__':
    main()