Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())
    P = [int(x) - 1 for x in input().split()]
    C = [int(x) for x in input().split()]

    ans = -10 ** 9
    # 全ての点を始点とした場合を考える(N通り)
    for start in range(N):
        # 始点を含むループを探す，スコアの累積和も勘定しておく
        now = start
        loop = []
        total = 0
        while now != start or not loop:
            loop.append(C[now])
            now = P[now]
            total += C[now]
        # ループごとの最大スコアを探す
        length = len(loop)
        t = 0
        for i in range(length):
            t += loop[i]
            # ループ長 > Kなら，ループ途中で探索終了
            if i + 1 > K:
                break
            # ループ長 <= Kなら，ループを周回するので
            # それを利用して探索を省略
            now = t
            if total > 0:
                e = (K - (i + 1)) // length
                now += total * e
            ans = max(ans, now)
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())
    P = [int(x) - 1 for x in input().split()]
    C = [int(x) for x in input().split()]

    ans = -10 ** 9
    for start in range(N):
        # ループを探す
        now = start
        loop = []
        total = 0
        while now != start or not loop:
            loop.append(C[now])
            now = P[now]
            total += C[now]
        # ループごとの最大スコアを探す
        length = len(loop)
        # sum_loop = sum(loop)
        # # ループ長よりも操作回数が多いか？，ループを周回すると得か？で場合分け
        # res = 0 if sum_loop <= 0 else sum_loop * (K // length)
        # limit = K if K <= length else K % length
        # for k in range(limit):
        #     res += loop[k]
        #     ans = max(ans, res)
        t = 0
        for i in range(length):
            t += loop[i]
            if i + 1 > K:
                break
            now = t
            if total > 0:
                e = (K - (i + 1)) // length
                now += total * e
            ans = max(ans, now)
    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())
    P = [int(x) - 1 for x in input().split()]
    C = [int(x) for x in input().split()]

    ans = -10 ** 9
    for start in range(N):
        # ループを探す
        now = start
        loop = []
        total = 0
        while now != start or not loop:
            loop.append(C[now])
            now = P[now]
            total += C[now]
        # ループごとの最大スコアを探す
        length = len(loop)
        # sum_loop = sum(loop)
        # # ループ長よりも操作回数が多いか？，ループを周回すると得か？で場合分け
        # res = 0 if sum_loop <= 0 else sum_loop * (K // length)
        # limit = K if K <= length else K % length
        # for k in range(limit):
        #     res += loop[k]
        #     ans = max(ans, res)
        t = 0
        for i in range(length):
            t += loop[i]
            if i + 1 > K:
                break
            now = t
            if total > 0:
                e = (K - (i + 1)) // length
                now += total * e
            ans = max(ans, now)
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())
    P = [int(x) - 1 for x in input().split()]
    C = [int(x) for x in input().split()]

    ans = -10 ** 9
    for start in range(N):
        # ループを探す
        now = start
        loop = []
        total = 0
        while now != start or not loop:
            loop.append(C[now])
            now = P[now]
            total += C[now]
        # ループごとの最大スコアを探す
        length = len(loop)
        # sum_loop = sum(loop)
        # # ループ長よりも操作回数が多いか？，ループを周回すると得か？で場合分け
        # res = 0 if sum_loop <= 0 else sum_loop * (K // length)
        # limit = K if K <= length else K % length
        # for k in range(limit):
        #     res += loop[k]
        #     ans = max(ans, res)
        t = 0
        for i in range(length):
            t += loop[i]
            if i + 1 > K:
                break
            now = t
            if total > 0:
                e = (K - (i + 1)) // length
                now += total * e
            ans = max(ans, now)
    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
def main():
    import sys

    sys.setrecursionlimit(10 ** 4)

    def search_loop(now: int, loop: list):
        """iから探索を初めて，再びiに達するまでに通った点のスコアをリストとして返す"""
        seen[now] = True
        if now == i and loop:
            loops.append(loop)
            return
        loop.append(C[now])
        search_loop(P[now], loop)

    N, K = map(int, input().split())
    P = [int(x) - 1 for x in input().split()]
    C = [int(x) for x in input().split()]

    # ループを探す
    seen = [False] * N
    loops = []
    for i in range(N):
        if not seen[i]:
            search_loop(i, [])

    # ループごとの最大スコアを探す
    ans = -10 ** 9
    for loop in loops:
        length = len(loop)
        sum_loop = sum(loop)
        # ループ長よりも操作回数が多いか？，ループを周回すると得か？で場合分け
        res = 0 if sum_loop <= 0 else sum_loop * (K // length)
        limit = K if K <= length else K % length

        loop_temp = loop * 2  # loopリストに同じloopリストを接続する
        for i in range(length):
            lst = [res]
            # cal = res
            for k in range(limit):
                # cal += loop_temp[i + k]
                # ans = max(ans, cal)
                lst.append(lst[-1] + loop_temp[i + k])
            ans = max(ans, max(lst[1:]))
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())
    P = [int(x) - 1 for x in input().split()]
    C = [int(x) for x in input().split()]

    ans = -10 ** 9
    for start in range(N):
        # ループを探す
        now = start
        loop = []
        total = 0
        while now != start or not loop:
            loop.append(C[now])
            now = P[now]
            total += C[now]
        # ループごとの最大スコアを探す
        length = len(loop)
        # sum_loop = sum(loop)
        # # ループ長よりも操作回数が多いか？，ループを周回すると得か？で場合分け
        # res = 0 if sum_loop <= 0 else sum_loop * (K // length)
        # limit = K if K <= length else K % length
        # for k in range(limit):
        #     res += loop[k]
        #     ans = max(ans, res)
        t = 0
        for i in range(length):
            t += loop[i]
            if i + 1 > K:
                break
            now = t
            if total > 0:
                e = (K - (i + 1)) // length
                now += total * e
            ans = max(ans, now)
    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
def main():
    def search_loop(now: int, loop: list):
        """iから探索を初めて，再びiに達するまでに通った点のスコアをリストとして返す"""
        seen[now] = True
        if now == i and loop:
            loops.append(loop)
            return
        loop.append(C[now])
        search_loop(P[now], loop)

    N, K = map(int, input().split())
    P = [int(x) - 1 for x in input().split()]
    C = [int(x) for x in input().split()]

    # ループを探す
    seen = [False] * N
    loops = []
    for i in range(N):
        if seen[i] is False:
            search_loop(i, [])

    # ループごとの最大スコアを探す
    ans = -10 ** 9
    for loop in loops:
        length = len(loop)
        sum_loop = sum(loop)
        # ループ長よりも操作回数が多く，ループを周回すると得な場合
        if K > length and sum_loop > 0:
            res = sum_loop * (K // length)
            limit = K % length
        # そうじゃない場合
        else:
            res = 0
            limit = K

        loop_temp = loop * 3  # loopリストに同じloopリストを接続する
        for k in range(1, limit + 1):
            for i in range(length):
                ans = max(ans, sum(loop_temp[i:i + k]) + res)
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())
    P = [int(x) - 1 for x in input().split()]
    C = [int(x) for x in input().split()]

    ans = -10 ** 9
    for start in range(N):
        # ループを探す
        now = start
        loop = []
        total = 0
        while now != start or not loop:
            loop.append(C[now])
            now = P[now]
            total += C[now]
        # ループごとの最大スコアを探す
        length = len(loop)
        # sum_loop = sum(loop)
        # # ループ長よりも操作回数が多いか？，ループを周回すると得か？で場合分け
        # res = 0 if sum_loop <= 0 else sum_loop * (K // length)
        # limit = K if K <= length else K % length
        # for k in range(limit):
        #     res += loop[k]
        #     ans = max(ans, res)
        t = 0
        for i in range(length):
            t += loop[i]
            if i + 1 > K:
                break
            now = t
            if total > 0:
                e = (K - (i + 1)) // length
                now += total * e
            ans = max(ans, now)
    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
def main():
    def search_loop(now: int, loop: list):
        """iから探索を初めて，再びiに達するまでに通った点のスコアをリストとして返す"""
        seen[now] = True
        if now == i and loop:
            loops.append(loop)
            return
        loop.append(C[now])
        search_loop(P[now], loop)

    N, K = map(int, input().split())
    P = [int(x) - 1 for x in input().split()]
    C = [int(x) for x in input().split()]

    # ループを探す
    seen = [False] * N
    loops = []
    for i in range(N):
        if seen[i] is False:
            search_loop(i, [])

    # ループごとの最大スコアを探す
    ans = -10 ** 9
    for loop in loops:
        length = len(loop)
        sum_loop = sum(loop)
        # ループ長よりも操作回数が多く，ループを周回すると得な場合
        if K > length and sum_loop > 0:
            res = sum_loop * (K // length)
            limit = K % length
        # そうじゃない場合
        else:
            res = 0
            limit = K

        loop_temp = loop * 2  # loopリストに同じloopリストを接続する
        for k in range(1, limit + 1):
            for i in range(length):
                ans = max(ans, sum(loop_temp[i:i + k]) + res)
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())
    P = [int(x) - 1 for x in input().split()]
    C = [int(x) for x in input().split()]

    ans = -10 ** 9
    for start in range(N):
        # ループを探す
        now = start
        loop = []
        total = 0
        while now != start or not loop:
            loop.append(C[now])
            now = P[now]
            total += C[now]
        # ループごとの最大スコアを探す
        length = len(loop)
        # sum_loop = sum(loop)
        # # ループ長よりも操作回数が多いか？，ループを周回すると得か？で場合分け
        # res = 0 if sum_loop <= 0 else sum_loop * (K // length)
        # limit = K if K <= length else K % length
        # for k in range(limit):
        #     res += loop[k]
        #     ans = max(ans, res)
        t = 0
        for i in range(length):
            t += loop[i]
            if i + 1 > K:
                break
            now = t
            if total > 0:
                e = (K - (i + 1)) // length
                now += total * e
            ans = max(ans, now)
    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
def main():
    # DFSでやる?
    def rec(now: int, score: int, cnt: int):
        nonlocal ans
        if cnt >= k:
            ans = max(ans, score)
            return
        seen[now] = True
        rec(P[now], score + C[P[now]], cnt + 1)

    N, K = map(int, input().split())
    P = [int(x) - 1 for x in input().split()]
    C = [int(x) for x in input().split()]

    seen = [False] * N
    ans = -(10 ** 9)
    for k in range(1, K + 1):
        for i in range(N):
            rec(i, 0, 0)
    print(ans)
    

if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python3
def main():
    N, K = map(int, input().split())
    P = [int(x) - 1 for x in input().split()]
    C = [int(x) for x in input().split()]

    ans = -10 ** 9
    for start in range(N):
        # ループを探す
        now = start
        loop = []
        total = 0
        while now != start or not loop:
            loop.append(C[now])
            now = P[now]
            total += C[now]
        # ループごとの最大スコアを探す
        length = len(loop)
        # sum_loop = sum(loop)
        # # ループ長よりも操作回数が多いか？，ループを周回すると得か？で場合分け
        # res = 0 if sum_loop <= 0 else sum_loop * (K // length)
        # limit = K if K <= length else K % length
        # for k in range(limit):
        #     res += loop[k]
        #     ans = max(ans, res)
        t = 0
        for i in range(length):
            t += loop[i]
            if i + 1 > K:
                break
            now = t
            if total > 0:
                e = (K - (i + 1)) // length
                now += total * e
            ans = max(ans, now)
    print(ans)


if __name__ == '__main__':
    main()