Buggy python submission (verdict: Time Limit Exceeded):
import sys
def main():    
    input = sys.stdin.readline
    n, k = map(int, input().split())
    p = list(map(int, input().split()))
    c = list(map(int, input().split()))
    l = []
    for i in range(n):
        cur = i
        l2 = []
        l3 = []
        score = 0
        cnt = 0
        while True:
            if p[i] - 1 in l2:
                score += c[p[i] - 1]
                l3.append(score)
                break
            l2.append(i)
            score += c[p[i] - 1]
            l3.append(score)
            i = p[i] - 1
            cnt += 1
        l.append([l3, cnt + 1])
    A = []
    for i in range(n):
        x, y = l[i][0], l[i][1]
        ans = 0
        if x[-1] < 0:
            if k >= y:
                ans = max(x)
            else:
                if k % y == 0:
                    pass
                else:
                    ans = max(x[:k % y])
        else:
            ans = x[-1] * (k // y)
            if k % y == 0:
                pass
            else:
                ans += max(x[:k % y])
        A.append(ans)
    print(max(A))
if __name__ == "__main__":
    main()

Fixed submission:
n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
l = [[] for i in range(n)]
for i in range(n):
    l2 = [i + 1]
    cnt = 0
    while True:
        l2.append(p[l2[-1] - 1])
        cnt += c[l2[-1] - 1]
        l[i].append(cnt)
        if l2[-1] == i + 1:
            break
ans = -float("inf")
for i in range(n):
    le = len(l[i])
    for j in range(min(k, le)):
        cnt = l[i][j]
        if l[i][-1] > 0:
            cnt += (k - j - 1) // le * l[i][-1]
        ans = max(ans, cnt)
print(ans)

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()))
l = []
for i in range(n):
    cur = i
    l2 = []
    l3 = []
    score = 0
    cnt = 0
    while True:
        if p[i] - 1 in l2:
            score += c[p[i] - 1]
            l3.append(score)
            break
        l2.append(i)
        score += c[p[i] - 1]
        l3.append(score)
        i = p[i] - 1
        cnt += 1
    l.append([l3, cnt + 1])
A = []
for i in range(n):
    x, y = l[i][0], l[i][1]
    ans = 0
    if x[-1] < 0:
        if k >= y:
            ans = max(x)
        else:
            if k % y == 0:
                pass
            else:
                ans = max(x[:k % y])
    else:
        ans = x[-1] * (k // y)
        if k % y == 0:
            pass
        else:
            ans += max(x[:k % y])
    A.append(ans)
print(max(A))

Fixed submission:
n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
l = [[] for i in range(n)]
for i in range(n):
    l2 = [i + 1]
    cnt = 0
    while True:
        l2.append(p[l2[-1] - 1])
        cnt += c[l2[-1] - 1]
        l[i].append(cnt)
        if l2[-1] == i + 1:
            break
ans = -float("inf")
for i in range(n):
    le = len(l[i])
    for j in range(min(k, le)):
        cnt = l[i][j]
        if l[i][-1] > 0:
            cnt += (k - j - 1) // le * l[i][-1]
        ans = max(ans, cnt)
print(ans)