import numpy as np
from numba import njit

@njit('i8(i8,i8,i8[:],i8[:],i8)', cache=True)
def main(N, K, plist, slist, max_score):
    for start in range(N):

        total_score_in_loop = 0
        score_loop = []
        now = start
        while True:
            now = plist[now]
            total_score_in_loop += slist[now]
            score_loop.append(slist[now])
            if now == start:
                break
        
        rem_score, loop_score = 0, 0
        for idx in range(1, len(score_loop)+1):
            rem_score += score_loop[idx-1]
            if total_score_in_loop > 0:
                loop_score = ((K - idx) // len(score_loop)) * total_score_in_loop
            max_score = max(max_score, rem_score + loop_score)
    return max_score


N, K = map(int, input().split())
plist = np.array([i-1 for i in map(int, input().split())], dtype='int64')
slist = np.array([i for i in map(int, input().split())], dtype='int64')
max_score = -10**15
max_score = main(N, K, plist, slist, max_score)
print(max_score) import numpy as np numpy from numba import njit def main(N, K, plist, slist, max_score):
    for start in range(N):

        total_score_in_loop = 0
        score_loop = []
        now = start
        while True:
            now = plist[now]
            total_score_in_loop += slist[now]
            score_loop.append(slist[now])
            if now == start:
                break
        
        rem_score, loop_score = 0, 0
        for idx in range(1, len(score_loop)+1):
            rem_score += score_loop[idx-1]
            if total_score_in_loop > 0:
                loop_score = ((K - idx) // len(score_loop)) * total_score_in_loop
            max_score = max(max_score, rem_score + loop_score)
    return max_score main for start in range(N):

        total_score_in_loop = 0
        score_loop = []
        now = start
        while True:
            now = plist[now]
            total_score_in_loop += slist[now]
            score_loop.append(slist[now])
            if now == start:
                break
        
        rem_score, loop_score = 0, 0
        for idx in range(1, len(score_loop)+1):
            rem_score += score_loop[idx-1]
            if total_score_in_loop > 0:
                loop_score = ((K - idx) // len(score_loop)) * total_score_in_loop
            max_score = max(max_score, rem_score + loop_score)
     start range(N) range N total_score_in_loop = 0 total_score_in_loop 0 score_loop = [] score_loop [] now = start now start while True:
            now = plist[now]
            total_score_in_loop += slist[now]
            score_loop.append(slist[now])
            if now == start:
                break
        
         True now = plist[now] now plist[now] plist now total_score_in_loop += slist[now] total_score_in_loop slist[now] slist now score_loop.append(slist[now]) score_loop.append score_loop append slist[now] slist now if now == start:
                break
        
         now == start now start break rem_score, loop_score = 0, 0 rem_score 0 loop_score 0 for idx in range(1, len(score_loop)+1):
            rem_score += score_loop[idx-1]
            if total_score_in_loop > 0:
                loop_score = ((K - idx) // len(score_loop)) * total_score_in_loop
            max_score = max(max_score, rem_score + loop_score)
     idx range(1, len(score_loop)+1) range 1 len(score_loop)+1 len(score_loop) len score_loop 1 rem_score += score_loop[idx-1] rem_score score_loop[idx-1] score_loop idx-1 idx 1 if total_score_in_loop > 0:
                loop_score = ((K - idx) // len(score_loop)) * total_score_in_loop
             total_score_in_loop > 0 total_score_in_loop 0 loop_score = ((K - idx) // len(score_loop)) * total_score_in_loop loop_score ((K - idx) // len(score_loop)) * total_score_in_loop (K - idx) // len(score_loop) K - idx K idx len(score_loop) len score_loop total_score_in_loop max_score = max(max_score, rem_score + loop_score) max_score max(max_score, rem_score + loop_score) max max_score rem_score + loop_score rem_score loop_score return max_score max_score N N K K plist plist slist slist max_score max_score @njit('i8(i8,i8,i8[:],i8[:],i8)', cache=True) main 'i8(i8,i8,i8[:],i8[:],i8)', cache=True @njit('i8(i8,i8,i8[:],i8[:],i8)', cache=True) njit 'i8(i8,i8,i8[:],i8[:],i8)' cache=True cache True main N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K i-1 for i in map(int, input().split()) for i in map(int, input().split()) i map(int, input().split()) map int input().split() ().split () input split for i in map(int, input().split()) i-1 i 1 plist = np.array([i-1 for i in map(int, input().split())], dtype='int64') plist np.array([i-1 for i in map(int, input().split())], dtype='int64') np.array np array [i-1 for i in map(int, input().split())] dtype='int64' dtype 'int64' i for i in map(int, input().split()) for i in map(int, input().split()) i map(int, input().split()) map int input().split() ().split () input split for i in map(int, input().split()) i slist = np.array([i for i in map(int, input().split())], dtype='int64') slist np.array([i for i in map(int, input().split())], dtype='int64') np.array np array [i for i in map(int, input().split())] dtype='int64' dtype 'int64' max_score = -10**15 max_score -10**15 -10 15 max_score = main(N, K, plist, slist, max_score) max_score main(N, K, plist, slist, max_score) main N K plist slist max_score print(max_score) print max_score max_score = -10**15 -10**15 max_score K = map(int, input().split()) map(int, input().split()) K max_score = main(N, K, plist, slist, max_score) main(N, K, plist, slist, max_score) max_score def main(N, K, plist, slist, max_score):
    for start in range(N):

        total_score_in_loop = 0
        score_loop = []
        now = start
        while True:
            now = plist[now]
            total_score_in_loop += slist[now]
            score_loop.append(slist[now])
            if now == start:
                break
        
        rem_score, loop_score = 0, 0
        for idx in range(1, len(score_loop)+1):
            rem_score += score_loop[idx-1]
            if total_score_in_loop > 0:
                loop_score = ((K - idx) // len(score_loop)) * total_score_in_loop
            max_score = max(max_score, rem_score + loop_score)
    return max_score def main(N, K, plist, slist, max_score):
    for start in range(N):

        total_score_in_loop = 0
        score_loop = []
        now = start
        while True:
            now = plist[now]
            total_score_in_loop += slist[now]
            score_loop.append(slist[now])
            if now == start:
                break
        
        rem_score, loop_score = 0, 0
        for idx in range(1, len(score_loop)+1):
            rem_score += score_loop[idx-1]
            if total_score_in_loop > 0:
                loop_score = ((K - idx) // len(score_loop)) * total_score_in_loop
            max_score = max(max_score, rem_score + loop_score)
    return max_score main slist = np.array([i for i in map(int, input().split())], dtype='int64') np.array([i for i in map(int, input().split())], dtype='int64') slist N, K = map(int, input().split()) map(int, input().split()) N plist = np.array([i-1 for i in map(int, input().split())], dtype='int64') np.array([i-1 for i in map(int, input().split())], dtype='int64') plist
import numpy as np
from numba import njit

@njit('i8(i8,i8,i8[:],i8[:],i8)', cache=True)
def main(N, K, plist, slist, max_score):
    for start in range(N):

        total_score_in_loop = 0
        score_loop = []
        now = start
        while True:
            now = plist[now]
            total_score_in_loop += slist[now]
            score_loop.append(slist[now])
            if now == start:
                break
        
        rem_score, loop_score = 0, 0
        for idx in range(1, len(score_loop)+1):
            rem_score += score_loop[idx-1]
            if total_score_in_loop > 0:
                loop_score = ((K - idx) // len(score_loop)) * total_score_in_loop
            max_score = max(max_score, rem_score + loop_score)
    return max_score


N, K = map(int, input().split())
plist = np.array([i-1 for i in map(int, input().split())], dtype='int64')
slist = np.array([i for i in map(int, input().split())], dtype='int64')
max_score = -10**15
max_score = main(N, K, plist, slist, max_score)
print(max_score)
