Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np


def solve(N, K, P, L):
    ans = np.iinfo(np.int64).min

    for i in range(N):
        score_acc = np.array([L[i]], dtype=np.int64)
        cur = P[i] - 1
        while cur != i:
            score_acc = np.append(score_acc, [L[cur]])
            cur = P[cur] - 1
        score_acc = np.cumsum(score_acc)
        M = len(score_acc)
        if M < K and score_acc[-1] > 0:
            res = K // M * score_acc[-1]
            if K % M:
                res += max(score_acc[: K % M])
            else:
                res += -score_acc[-1] + max(score_acc)
            ans = max(ans, res)
        else:
            res = max(score_acc[:K])
            ans = max(ans, res)
    return ans


N, K = map(int, input().split())
P = np.array(input().split(), dtype=np.int32)
L = np.array(input().split(), dtype=np.int32)
print(solve(N, K, P, L))


Fixed submission:
from itertools import accumulate


def solve():
    N, K = map(int, input().split())
    P = list(map(int, input().split()))
    L = list(map(int, input().split()))

    ans = -float("inf")
    for i in range(N):
        score_acc = [L[i]]
        cur = P[i] - 1
        while cur != i:
            score_acc.append(L[cur])
            cur = P[cur] - 1
        score_acc = list(accumulate(score_acc))
        M = len(score_acc)
        if M < K and score_acc[-1] > 0:
            res = K // M * score_acc[-1]
            if K % M:
                res += max(score_acc[: K % M])
            else:
                res += -score_acc[-1] + max(score_acc)
            ans = max(ans, res)
        else:
            res = max(score_acc[:K])
            ans = max(ans, res)
    return ans


print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
from numba import njit


@njit("i8(i4,i4,i4[:],i4[:])")
def solve(N, K, P, L):
    ans = -np.iinfo(np.int64).min

    for i in range(N):
        score_acc = np.array([L[i]], dtype=np.int64)
        cur = P[i] - 1
        while cur != i:
            score_acc = np.append(score_acc, [L[cur]])
            cur = P[cur] - 1
        score_acc = np.cumsum(score_acc)
        M = len(score_acc)
        if M < K and score_acc[-1] > 0:
            res = K // M * score_acc[-1]
            if K % M:
                res += max(score_acc[: K % M])
            else:
                res += -score_acc[-1] + max(score_acc)
            ans = max(ans, res)
        else:
            res = max(score_acc[:K])
            ans = max(ans, res)
    return ans


N, K = map(int, input().split())
P = np.array(input().split(), dtype=np.int32)
L = np.array(input().split(), dtype=np.int32)
print(solve(N, K, P, L))


Fixed submission:
from itertools import accumulate


def solve():
    N, K = map(int, input().split())
    P = list(map(int, input().split()))
    L = list(map(int, input().split()))

    ans = -float("inf")
    for i in range(N):
        score_acc = [L[i]]
        cur = P[i] - 1
        while cur != i:
            score_acc.append(L[cur])
            cur = P[cur] - 1
        score_acc = list(accumulate(score_acc))
        M = len(score_acc)
        if M < K and score_acc[-1] > 0:
            res = K // M * score_acc[-1]
            if K % M:
                res += max(score_acc[: K % M])
            else:
                res += -score_acc[-1] + max(score_acc)
            ans = max(ans, res)
        else:
            res = max(score_acc[:K])
            ans = max(ans, res)
    return ans


print(solve())