Buggy python submission (verdict: Runtime Error):
n,k = map(int,input().split())
pn = list(map(int,input().split()))
cn = list(map(int,input().split()))
ans = -float("inf")


for i in range(n): 
    start=i
    now=start
    total = 0 #ループ計
    ls = [0]

    for j in range(1, k+1):
        now = pn[now]-1
        total += cn[now]
        ans = max(ans, total)
        ls.append(total)
        if now == start:
            rp = j
            break
    else:
        continue  #roopなし

    if total <= 0:
        continue
    else:
        if k % rp == 0:
            ans2 = total * (k // rp-1) + max(ls)
            ans = max(ans, ans2)
        else:

            ans2 = total * (k//rp) + max(ls[:s+1])
            ans = max(ans, ans2)
print(ans)


Fixed submission:
n,k = map(int,input().split())
pn = list(map(int,input().split()))
cn = list(map(int,input().split()))
ans = -float("inf")


for i in range(n): 
    start=i
    now=start
    total = 0 #ループ計
    ls = [0]

    for j in range(1, k+1):
        now = pn[now]-1
        total += cn[now]
        ans = max(ans, total)
        ls.append(total)
        if now == start:
            rp = j
            break
    else:
        continue  #roopなし

    if total <= 0:
        continue
    else:
        if k % rp == 0:
            ans2 = total * (k // rp-1) + max(ls)
            ans = max(ans, ans2)
        else:
            ans2 = total * (k//rp) + max(ls[:k%rp+1])
            ans = max(ans, ans2)
print(ans)

Buggy python submission (verdict: Runtime Error):
# n=int(input())
n,k=map(int,input().split())
P=list(map(int,input().split()))
P=[i-1 for i in P]
C=list(map(int,input().split()))
ans=-float("inf")
for i in range(n):
    dp=[-float("inf")]*(n+2)
    kai=0
    dp[kai]=C[P[i]]
    now=P[i]
    bef=i
    while now!=i:
        kai+=1
        if kai>=k:break
        dp[kai]= dp[kai-1]+C[P[now]]
        now=P[now]
    #if dp[0]==max(dp):dp[0]=-float("INF")

    if kai>=k: ans=max(ans,max(dp));continue
    kai+=1
    t=k%kai
    ans=max(ans, dp[kai-1]*(k//kai) + max(dp[:t]) )
    
print(ans)
    


Fixed submission:
n,k = map(int,input().split())
pn = list(map(int,input().split()))
cn = list(map(int,input().split()))
ans = -float("inf")


for i in range(n): 
    start=i
    now=start
    total = 0 #ループ計
    ls = [0]

    for j in range(1, k+1):
        now = pn[now]-1
        total += cn[now]
        ans = max(ans, total)
        ls.append(total)
        if now == start:
            rp = j
            break
    else:
        continue  #roopなし

    if total <= 0:
        continue
    else:
        if k % rp == 0:
            ans2 = total * (k // rp-1) + max(ls)
            ans = max(ans, ans2)
        else:
            ans2 = total * (k//rp) + max(ls[:k%rp+1])
            ans = max(ans, ans2)
print(ans)

Buggy python submission (verdict: Runtime Error):
# n=int(input())
n,k=map(int,input().split())
P=list(map(int,input().split()))
P=[i-1 for i in P]
C=list(map(int,input().split()))
ans=-float("inf")
for i in range(n):
    dp=[-float("inf")]*(k+1)
    kai=0
    dp[kai]=C[P[i]]
    now=P[i]
    bef=i
    while 1:
        kai+=1
        if kai>=k:break
        dp[kai]= dp[kai-1]+C[P[now]]
        now=P[now]
    #if dp[0]==max(dp):dp[0]=-float("INF")
    ans=max(ans,max(dp))
print(ans)
    

Fixed submission:
n,k = map(int,input().split())
pn = list(map(int,input().split()))
cn = list(map(int,input().split()))
ans = -float("inf")


for i in range(n): 
    start=i
    now=start
    total = 0 #ループ計
    ls = [0]

    for j in range(1, k+1):
        now = pn[now]-1
        total += cn[now]
        ans = max(ans, total)
        ls.append(total)
        if now == start:
            rp = j
            break
    else:
        continue  #roopなし

    if total <= 0:
        continue
    else:
        if k % rp == 0:
            ans2 = total * (k // rp-1) + max(ls)
            ans = max(ans, ans2)
        else:
            ans2 = total * (k//rp) + max(ls[:k%rp+1])
            ans = max(ans, ans2)
print(ans)