N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = -float('inf')
v = []
for n in range(N):
    loop, score = 0, 0
    t = n
    for i in range(min(N,K)):
        n = P[n] - 1
        score += C[n]
        ans = max(ans,score)
        if n == t:
            loop = i + 1
            break
    v.append((loop,score))
    
    
for n in range(N):
    loop,score = v[n]
    if loop > 0 and score > 0:
        r = (K-1)//loop
        nscore = r*score
        for i in range(K - r*loop):
            n = P[n] - 1
            nscore += C[n]
            ans = max(nscore, ans)
            
print(ans) N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K P = list(map(int, input().split())) P list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split C = list(map(int, input().split())) C list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split ans = -float('inf') ans -float('inf') float('inf') float 'inf' v = [] v [] for n in range(N):
    loop, score = 0, 0
    t = n
    for i in range(min(N,K)):
        n = P[n] - 1
        score += C[n]
        ans = max(ans,score)
        if n == t:
            loop = i + 1
            break
    v.append((loop,score))
    
     n range(N) range N loop, score = 0, 0 loop 0 score 0 t = n t n for i in range(min(N,K)):
        n = P[n] - 1
        score += C[n]
        ans = max(ans,score)
        if n == t:
            loop = i + 1
            break
     i range(min(N,K)) range min(N,K) min N K n = P[n] - 1 n P[n] - 1 P[n] P n 1 score += C[n] score C[n] C n ans = max(ans,score) ans max(ans,score) max ans score if n == t:
            loop = i + 1
            break
     n == t n t loop = i + 1 loop i + 1 i 1 break v.append((loop,score)) v.append v append (loop,score) loop score for n in range(N):
    loop,score = v[n]
    if loop > 0 and score > 0:
        r = (K-1)//loop
        nscore = r*score
        for i in range(K - r*loop):
            n = P[n] - 1
            nscore += C[n]
            ans = max(nscore, ans)
             n range(N) range N loop,score = v[n] loop v[n] v n score if loop > 0 and score > 0:
        r = (K-1)//loop
        nscore = r*score
        for i in range(K - r*loop):
            n = P[n] - 1
            nscore += C[n]
            ans = max(nscore, ans)
             loop > 0 and score > 0 loop > 0 loop 0 score > 0 score 0 r = (K-1)//loop r (K-1)//loop K-1 K 1 loop nscore = r*score nscore r*score r score for i in range(K - r*loop):
            n = P[n] - 1
            nscore += C[n]
            ans = max(nscore, ans)
             i range(K - r*loop) range K - r*loop K r*loop r loop n = P[n] - 1 n P[n] - 1 P[n] P n 1 nscore += C[n] nscore C[n] C n ans = max(nscore, ans) ans max(nscore, ans) max nscore ans print(ans) print ans K = map(int, input().split()) map(int, input().split()) K ans = max(nscore, ans) max(nscore, ans) ans v = [] [] v loop,score = v[n] v[n] loop P = list(map(int, input().split())) list(map(int, input().split())) P score += C[n] C[n] score C = list(map(int, input().split())) list(map(int, input().split())) C n = P[n] - 1 P[n] - 1 n ans = -float('inf') -float('inf') ans nscore = r*score r*score nscore t = n n t loop = i + 1 i + 1 loop r = (K-1)//loop (K-1)//loop r N, K = map(int, input().split()) map(int, input().split()) N nscore += C[n] C[n] nscore n = P[n] - 1 P[n] - 1 n score = v[n] v[n] score score = 0, 0 0 score loop, score = 0, 0 0 loop ans = max(ans,score) max(ans,score) ans
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = -float('inf')
v = []
for n in range(N):
    loop, score = 0, 0
    t = n
    for i in range(min(N,K)):
        n = P[n] - 1
        score += C[n]
        ans = max(ans,score)
        if n == t:
            loop = i + 1
            break
    v.append((loop,score))
    
    
for n in range(N):
    loop,score = v[n]
    if loop > 0 and score > 0:
        r = (K-1)//loop
        nscore = r*score
        for i in range(K - r*loop):
            n = P[n] - 1
            nscore += C[n]
            ans = max(nscore, ans)
            
print(ans)