Problem p02585 (python)

Fixed submission:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import accumulate
from collections import deque, defaultdict
def main():
    n, ko = map(int, input().split())
    p = tuple(map(int, input().split()))
    c = tuple(map(int, input().split()))

    scores_all = deque()
    for k1 in range(n):
        scores = deque()
        kita = defaultdict(int)
        kita[k1] = ko
        cnt = ko - 1
        koko = p[k1] - 1
        scores.append(c[koko])
        kita[koko] = cnt
        while cnt:
            cnt -= 1
            koko = p[koko] - 1
            scores.append(c[koko])
            if kita[koko]:
                break
            else:
                kita[koko] = cnt
        cycle_long = kita[koko] - cnt
        scores_a = tuple(accumulate(scores))

        if cnt:
            if scores_a[-1] > 0:
                kaisuu = ko // cycle_long
                s1 = scores_a[-1] * kaisuu
                amari = ko % cycle_long
                if amari:
                    t = max(0, max(scores_a[:amari]))
                    s1 += t
                s2 = scores_a[-1] * (kaisuu - 1)
                s2 += max(scores_a)
                scores_all.append(max(s1, s2))
            else:
                scores_all.append(max(scores_a))
        else:
            scores_all.append(max(scores_a))
    print(max(scores_all))

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):

import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import accumulate
def main():
    n, k = map(int, input().split())
    p = tuple(map(int, input().split()))
    c = tuple(map(int, input().split()))

    seen = [0] * n
    cycles = []
    for i1 in range(n):
        if not seen[i1]:
            cycle = []
            koko = i1
            k2 = k - 1
            while k2:
                koko = p[koko] - 1
                cycle.append(c[koko])
                seen[koko] = 1
                if koko == i1:
                    cycles.append((cycle, True, sum(cycle)))
                    break
                k2 -= 1
            if k2 == 0:
                cycles.append((cycle, False, sum(cycle)))
    score_each_cycle = []
    for cycle in cycles:
        if cycle[1]:
            cycle0 = cycle[0]
            cycle_l = len(cycle0)
            kaisu, rem = divmod(k, cycle_l)
            scores = [0] * cycle_l
            scores[0] = cycle0[0]
            cycle0.extend(cycle0)
            cycle0 = [0] + cycle0
            cycle0 = tuple(accumulate(cycle0))
            for i1 in range(cycle_l):
                score = cycle0[1]
                for i2 in range(1, cycle_l+1):
                    score = max(score, cycle0[i1+i2]-cycle0[i1])
                scores[i1] = score
            if cycle[2] > 0:
                for j1 in range(rem + 1):
                    scores[j1] += kaisu * cycle[2]
            score_each_cycle.append((max(scores)))
        else:
            cycle0 = cycle[0]
            cycle_l = len(cycle[0])
            scores = [0] * cycle_l
            cycle0 = [0] + cycle0
            cycle0 = tuple(accumulate(cycle0))
            score = cycle0[1]
            for i1 in range(cycle_l):
                for i2 in range(1, cycle_l - i1):
                    score = max(score, cycle0[i1+i2]-cycle0[i1])
            score_each_cycle.append(score)
    r = max(max(c), max(score_each_cycle))
    print(r)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import accumulate
def main():
    n, ko = map(int, input().split())
    p = tuple(map(int, input().split()))
    c = tuple(map(int, input().split()))

    scores_all = set()
    for k1 in range(n):
        scores = []
        kita = [0] * n
        cnt = ko
        koko = k1
        kita[koko] = cnt
        cycle_long = n
        cnt -= 1
        koko = p[koko] - 1
        while cnt:
            if kita[koko]:
                cycle_long = kita[koko] - cnt
                scores.append(c[koko])
                break
            else:
                scores.append(c[koko])
                kita[koko] = cnt
                koko = p[koko] - 1
                cnt -= 1
        scores_a = tuple(accumulate(scores))
        if cnt:
            if scores_a[-1] > 0:
                kaisuu = ko // cycle_long
                s = scores_a[-1] * kaisuu
                amari = ko % cycle_long
                if amari:
                    s += max(scores_a[:amari])
                scores_all.add(s)
            else:
                scores_all.add(max(scores_a))
        else:
            scores_all.add(max(scores_a))
    print(max(scores_all))

if __name__ == '__main__':
    main()