Buggy python submission (verdict: Runtime Error):
# ABC175 D
import bisect

N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))

F=[0]*N

def op(n):
    C_n=[]
    P_n=[]
    m=n
    while 1:
        C_n.append(C[m-1])
        P_n.append(m)
        F[m-1]=1
        m=P[m-1]
        if m==n:
            break
    sum_C_n=sum(C_n)
    C_n=C_n[:]+C_n[:]
    n=len(P_n)
    res=[]
    for k in range(1,min(n+1,K)):
        res_k=[]
        sum_k=sum(C_n[:k])
        p=sum_k
        for i in range(n):
            sum_k+=C_n[i+k]-C_n[i]
            p=max(p,sum_k)
        if K-k>=n and sum_C_n>=0:
                p+=sum_C_n*(K-k)//n
        res.append(p)
    return max(res)
    
res=-float('inf')
while 0 in F:
    n=bisect.bisect_right(F,0)
    res=max(op(n),res)
print(res)

Fixed submission:
# ABC175 D
import bisect

N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))

F=[0]*N

def search(n):
    res=[]
    l=0
    m=n
    tmp_res=0
    isloop=0
    if F[n-1]:
        s,L=F[n-1]
    while 1:
        m=P[m-1]
        tmp_res+=C[m-1]
        l+=1
        if F[n-1]==0:
            res.append((tmp_res,l))
        else:
            res.append(tmp_res+s*((K-l)//L) if s>0 and K>=l else tmp_res)
        if m==n:
            isloop=1
            break
        if l==K:
            break
    if F[n-1]==0:
        if isloop:
            s,L=res[-1]
            if not F[n-1]:
                m=n
                while 1:
                    F[m-1]=(s,L)
                    m=P[m-1]
                    if m==n:
                        break
        else:
            s=-1
        p=max([p+s*((K-k)//len(res)) if s>0 and K>=k else p for p,k in res])
    else:
        p=max(res)
    return p

res=-float('inf')
for i in range(N):
    res=max(res,search(i+1))
print(res)

Buggy python submission (verdict: Runtime Error):
# ABC175 D
import bisect

N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))

F=[0]*N

def op(n):
    C_n=[]
    P_n=[]
    m=n
    while 1:
        C_n.append(C[m-1])
        P_n.append(m)
        F[m-1]=1
        m=P[m-1]
        if m==n:
            break
    sum_C_n=sum(C_n)
    C_n=C_n[:]+C_n[:]
    n=len(P_n)
    res=[]
    for k in range(1,min(n+1,K)):
        res_k=[]
        for i in range(n):
            res_k.append(sum(C_n[i:i+k]))
        p=max(res_k)
        if K-k>=n and sum_C_n>=0:
                p+=sum_C_n*(K-k)//n
        res.append(p)
    return max(res)
    
res=-float('inf')
while 0 in F:
    n=bisect.bisect_right(F,0)
    res=max(op(n),res)
print(res)

Fixed submission:
# ABC175 D
import bisect

N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))

F=[0]*N

def search(n):
    res=[]
    l=0
    m=n
    tmp_res=0
    isloop=0
    if F[n-1]:
        s,L=F[n-1]
    while 1:
        m=P[m-1]
        tmp_res+=C[m-1]
        l+=1
        if F[n-1]==0:
            res.append((tmp_res,l))
        else:
            res.append(tmp_res+s*((K-l)//L) if s>0 and K>=l else tmp_res)
        if m==n:
            isloop=1
            break
        if l==K:
            break
    if F[n-1]==0:
        if isloop:
            s,L=res[-1]
            if not F[n-1]:
                m=n
                while 1:
                    F[m-1]=(s,L)
                    m=P[m-1]
                    if m==n:
                        break
        else:
            s=-1
        p=max([p+s*((K-k)//len(res)) if s>0 and K>=k else p for p,k in res])
    else:
        p=max(res)
    return p

res=-float('inf')
for i in range(N):
    res=max(res,search(i+1))
print(res)