Buggy python submission (verdict: Time Limit Exceeded):
def numba_compile(numba_config):
    import os, sys
    if sys.argv[-1] == "ONLINE_JUDGE":
        from numba import njit
        from numba.pycc import CC
        cc = CC("my_module")
        for func, signature in numba_config:
            globals()[func.__name__] = njit(signature)(func)
            cc.export(func.__name__, signature)(func)
        cc.compile()
        exit()
    elif os.name == "posix":
        exec(f"from my_module import {','.join(func.__name__ for func, _ in numba_config)}")
        for func, _ in numba_config:
            globals()[func.__name__] = vars()[func.__name__]
    else:
        from numba import njit
        for func, signature in numba_config:
            globals()[func.__name__] = njit(signature, cache=True)(func)
        print("compiled!", file=sys.stderr)

import numpy as np

def solve(N, K, P, C):
    ans = -10**18
    for start in range(N):
        t = 1
        s = 0
        idx = start
        while P[idx] != start:
            idx = P[idx]
            s += C[idx]
            t += 1
        s += C[start]
        q, m = divmod(K, t)
        #print(R)
        score = 0
        #print(ans, s)
        if s >= 0:
            x = max(0, (q-2)*t)
            score += s * (x//t)
            #print(f"score={score}")
            idx = start
            for _ in range(x, K):
                idx = P[idx]
                score += C[idx]
                ans = max(ans, score)
        else:
            idx = start
            for _ in range(1, K+1):
                idx = P[idx]
                score += C[idx]
                ans = max(ans, score)
    print(ans)

numba_compile([
    [solve, "void(i8,i8,i8[:],i8[:])"],
])


def main():
    N, K = map(int, input().split())
    P = np.array(input().split(), dtype=np.int64)-1
    C = np.array(input().split(), dtype=np.int64)
    solve(N, K, P, C)

main()


Fixed submission:
def numba_compile(numba_config):
    import os, sys
    if sys.argv[-1] == "ONLINE_JUDGE":
        from numba import njit
        from numba.pycc import CC
        cc = CC("my_module")
        for func, signature in numba_config:
            globals()[func.__name__] = njit(signature)(func)
            cc.export(func.__name__, signature)(func)
        cc.compile()
        exit()
    elif os.name == "posix":
        exec(f"from my_module import {','.join(func.__name__ for func, _ in numba_config)}")
        for func, _ in numba_config:
            globals()[func.__name__] = vars()[func.__name__]
    else:
        from numba import njit
        for func, signature in numba_config:
            globals()[func.__name__] = njit(signature, cache=True)(func)
        print("compiled!", file=sys.stderr)

import numpy as np

def solve(N, K, P, C):
    ans = -10**18
    for start in range(N):
        t = 1
        s = 0
        idx = start
        while P[idx] != start:
            idx = P[idx]
            s += C[idx]
            t += 1
        s += C[start]
        q = K // t
        #print(R)
        score = 0
        #print(ans, s)
        if s >= 0:
            x = max(0, (q-2)*t)
            score += s * (x//t)
            #print(f"score={score}")
            idx = start
            for _ in range(x, K):
                idx = P[idx]
                score += C[idx]
                ans = max(ans, score)
        else:
            idx = start
            for _ in range(1, min(N, K+1)):
                idx = P[idx]
                score += C[idx]
                ans = max(ans, score)
    return ans

numba_compile([
    [solve, "i8(i8,i8,i8[:],i8[:])"],
])


def main():
    N, K = map(int, input().split())
    P = np.array(input().split(), dtype=np.int64)-1
    C = np.array(input().split(), dtype=np.int64)
    ans = solve(N, K, P, C)
    print(ans)

main()