Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
P = list(map(lambda s: int(s) - 1, input().split()))
C = list(map(int, input().split()))

ans = -1_000_000_000
for i in range(N):
    S = [C[i]]
    p = P[i]
    while p != i:
        S.append(C[p])
        p = P[p]
    s = sum(S)
    n = len(S)
    acc = 0
    for i in range(min(n, K + 2)):
        acc += S[i]
        a = acc
        if s > 0:
            a += s * ((K - (i + 1)) // n)
        ans = max(a, ans)

print(ans)


Fixed submission:
N, K = map(int, input().split())
P = list(map(lambda s: int(s) - 1, input().split()))
C = list(map(int, input().split()))

ans = -1_000_000_000
for i in range(N):
    S = [C[i]]
    p = P[i]
    while p != i:
        S.append(C[p])
        p = P[p]
    s = sum(S)
    n = len(S)
    acc = 0
    for i in range(min(n, K + 2)):
        acc += S[i]
        a = acc
        if s > 0:
            a += s * ((K - (i + 1)) // n)
        ans = max(a, ans)

print(ans)

Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
P = list(map(lambda s: int(s) - 1, input().split()))
C = list(map(int, input().split()))

visited = [False] * N
ans = []
for i in range(N):
    if visited[i]:
        continue
    visited[i] = True
    S = [C[i]]
    p = P[i]
    while p != i:
        visited[p] = True
        S.append(C[p])
        p = P[p]
    s = sum(S)
    n = len(S)
    SS = S + S
    if s <= 0 or K <= n:
        a = max(sum(SS[j:j + c + 1])
                for j in range(n) for c in range(min(K, n)))
    else:
        M = max(sum(SS[j:j + c + 1]) for j in range(n) for c in range(K % n))
        a = s * (K // n) + M
    ans.append(a)

print(max(ans))


Fixed submission:
N, K = map(int, input().split())
P = list(map(lambda s: int(s) - 1, input().split()))
C = list(map(int, input().split()))

ans = -1_000_000_000
for i in range(N):
    S = [C[i]]
    p = P[i]
    while p != i:
        S.append(C[p])
        p = P[p]
    s = sum(S)
    n = len(S)
    acc = 0
    for i in range(min(n, K + 2)):
        acc += S[i]
        a = acc
        if s > 0:
            a += s * ((K - (i + 1)) // n)
        ans = max(a, ans)

print(ans)

Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
P = list(map(lambda s: int(s) - 1, input().split()))
C = list(map(int, input().split()))

visited = [False] * N
ans = []
for i in range(N):
    if visited[i]:
        continue
    visited[i] = True
    S = [C[i]]
    p = P[i]
    while p != i:
        visited[p] = True
        S.append(C[p])
        p = P[p]
    s = sum(S)
    n = len(S)
    SS = S + S
    if s <= 0 or K <= n:
        a = max(sum(SS[j:j + c + 1])
                for j in range(n) for c in range(min(K, n)))
    else:
        M = max(sum(SS[j:j + c + 1]) for j in range(n) for c in range(K % n))
        a = s * (K // n) + M
    ans.append(a)

print(max(ans))


Fixed submission:
N, K = map(int, input().split())
P = list(map(lambda s: int(s) - 1, input().split()))
C = list(map(int, input().split()))

ans = -1_000_000_000
for i in range(N):
    S = [C[i]]
    p = P[i]
    while p != i:
        S.append(C[p])
        p = P[p]
    s = sum(S)
    n = len(S)
    acc = 0
    for i in range(min(n, K + 2)):
        acc += S[i]
        a = acc
        if s > 0:
            a += s * ((K - (i + 1)) // n)
        ans = max(a, ans)

print(ans)

Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
P = list(map(lambda s: int(s) - 1, input().split()))
C = list(map(int, input().split()))

visited = [False] * N
ans = []
for i in range(N):
    if visited[i]:
        continue
    visited[i] = True
    S = [C[i]]
    p = P[i]
    while p != i:
        visited[p] = True
        S.append(C[p])
        p = P[p]
    s = sum(S)
    n = len(S)
    SS = S + S
    if s <= 0 or K < n:
        a = max(sum(SS[j:j + c + 1])
                for j in range(n) for c in range(min(K, n)))
    else:
        M = max(sum(SS[j:j + c + 1]) for j in range(n) for c in range(K % n))
        a = s * (K // n) + M
    ans.append(a)

print(max(ans))


Fixed submission:
N, K = map(int, input().split())
P = list(map(lambda s: int(s) - 1, input().split()))
C = list(map(int, input().split()))

ans = -1_000_000_000
for i in range(N):
    S = [C[i]]
    p = P[i]
    while p != i:
        S.append(C[p])
        p = P[p]
    s = sum(S)
    n = len(S)
    acc = 0
    for i in range(min(n, K + 2)):
        acc += S[i]
        a = acc
        if s > 0:
            a += s * ((K - (i + 1)) // n)
        ans = max(a, ans)

print(ans)

Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
P = list(map(lambda s: int(s) - 1, input().split()))
C = list(map(int, input().split()))

visited = [False] * N
ans = []
for i in range(N):
    if visited[i]:
        continue
    visited[i] = True
    S = [C[i]]
    p = P[i]
    while p != i:
        visited[p] = True
        S.append(C[p])
        p = P[p]
    s = sum(S)
    n = len(S)
    SS = S + S
    if s <= 0 or K < n:
        a = max(sum(SS[j:j + c + 1])
                for j in range(N) for c in range(min(K, n)))
    else:
        M = max(sum(SS[j:j + c + 1]) for j in range(N) for c in range(K % n))
        a = s * (K // n) + M
    ans.append(a)

print(max(ans))


Fixed submission:
N, K = map(int, input().split())
P = list(map(lambda s: int(s) - 1, input().split()))
C = list(map(int, input().split()))

ans = -1_000_000_000
for i in range(N):
    S = [C[i]]
    p = P[i]
    while p != i:
        S.append(C[p])
        p = P[p]
    s = sum(S)
    n = len(S)
    acc = 0
    for i in range(min(n, K + 2)):
        acc += S[i]
        a = acc
        if s > 0:
            a += s * ((K - (i + 1)) // n)
        ans = max(a, ans)

print(ans)