Buggy python submission (verdict: Time Limit Exceeded):
n , k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))
loop = []
visit = [False for i in range(n)]
for i in range(n):
    if visit[i]:
        continue
    tyu  = [i]
    now = i
    visit[i] = True
    while True:
        nex = p[now] - 1
        if nex == i:
            break
        visit[nex] = True
        tyu.append(nex)
        now = nex
    loop.append(tyu)

ans = []

for p in loop:
    kouho = []
    losum = 0
    lokz = len(p)
    kar = []
    for i in p:
        losum += c[i]
        kar.append(c[i])
    if losum > 0:
        kouho.append(losum*(k//lokz))
        for i in range(lokz):
            for j in range(lokz):
                if i < j:
                    amari = j - i
                    if amari <= k:
                        cou = losum*((k-amari)//lokz) + sum(kar[i:j])
                elif i == j:
                    continue
                elif i > j:
                    amari = lokz - (i-j)
                    if amari <= k:
                        cou = losum*((k-amari)//lokz) + sum(kar[:j]) + sum(kar[i:])
                kouho.append(cou)
    elif losum <= 0:
        for i in range(lokz):
            for j in range(lokz):
                if i <= j:
                    amari = j - i
                    if amari <= k:
                        cou = sum(kar[i:j+1])
                elif i > j:
                    amari = lokz - (i-j)
                    if amari <= k:
                        cou = sum(kar[:j+1]) + sum(kar[i:])
                kouho.append(cou)
    ans.append(max(kouho))
print(max(ans))
    

Fixed submission:
n , k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))
loop = []
visit = [(-1,-1) for i in range(n)]
cou = -1
for i in range(n):
    if visit[i] != (-1,-1):
        continue
    cou += 1
    tyu  = [i]
    now = i
    visit[i] = (cou,0)
    t = 1
    while True:
        nex = p[now] - 1
        if nex == i:
            break
        visit[nex] = (cou,t)
        tyu.append(nex)
        now = nex
        t += 1
    loop.append(tyu)

ans = []

for i in range(n):
    p = loop[visit[i][0]][visit[i][1]:] + loop[visit[i][0]][:visit[i][1]]
    kar = [0]
    kouho = []
    losum = 0
    lokz = len(p)
    for j in p:
        losum += c[j]
        kar.append(kar[-1] + c[j])
    if losum >= 0:
        for l in range(lokz+1):
            if l > k:
                break
            cou = losum*((k-l)//lokz) + kar[l]
            kouho.append(cou)
    elif losum < 0:
        for l in range(1,lokz+1):
            if l > k:
                break
            cou = kar[l]
            kouho.append(cou)
    ans.append(max(kouho))
print(max(ans))

Buggy python submission (verdict: Runtime Error):
n , k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))
ans = []
for i in range(n):
    visit = [-1 for i in range(n)]
    cou = 0
    visit[i] = 0
    kar = []
    now = i
    while True:
        cou += 1
        if cou > k:
            break
        nex = p[now] - 1
        if visit[nex] == -1:
            visit[nex] = cou
            if kar:
                kar.append(kar[-1] + c[nex])
            else:
                kar.append(c[nex])
            now = nex
        elif visit[nex] != -1:
            if kar:
                lokz = cou
                losu = kar[-1] + c[nex]
                if losu > 0:
                    kar.append(losu * (k//lokz) + max(kar[:(k%lokz)]))
                break
            else:
                if c[nex] >= 0:
                    kar.append(c[nex]*k)
                elif c[nex] < 0:
                    kar.append(c[nex])
                break
    ans.append(max(kar))
print(max(ans))

Fixed submission:
n , k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))
loop = []
visit = [(-1,-1) for i in range(n)]
cou = -1
for i in range(n):
    if visit[i] != (-1,-1):
        continue
    cou += 1
    tyu  = [i]
    now = i
    visit[i] = (cou,0)
    t = 1
    while True:
        nex = p[now] - 1
        if nex == i:
            break
        visit[nex] = (cou,t)
        tyu.append(nex)
        now = nex
        t += 1
    loop.append(tyu)

ans = []

for i in range(n):
    p = loop[visit[i][0]][visit[i][1]:] + loop[visit[i][0]][:visit[i][1]]
    kar = [0]
    kouho = []
    losum = 0
    lokz = len(p)
    for j in p:
        losum += c[j]
        kar.append(kar[-1] + c[j])
    if losum >= 0:
        for l in range(lokz+1):
            if l > k:
                break
            cou = losum*((k-l)//lokz) + kar[l]
            kouho.append(cou)
    elif losum < 0:
        for l in range(1,lokz+1):
            if l > k:
                break
            cou = kar[l]
            kouho.append(cou)
    ans.append(max(kouho))
print(max(ans))