#D - Moving_piece
N,K = map(int,input().split())
P =[]
C =[]
P = list(map(int,input().split()))
C = list(map(int,input().split()))

score_output = -1000000000
i = 0
for i in range(N):
    ichi = i
    score = 0
    step  = 0
    loop  = 0
    score_max = -1000000000
    while True:
        step  += 1
        score += C[ichi]
        ichi   = (P[ichi] -1)
        if i ==  (P[ichi] -1):
            score += C[ichi]
            break
        
    path = 0
    cnt  = 0
    
    while True:
        cnt  += 1
        path += C[ichi]
        
        if cnt > K:
            break
        
        loop = int((K - cnt) / (step+ 1))
        if score > 0:
            score_max = path + loop * score
        else:
            score_max = path
        if score_max > score_output:
            score_output = score_max

        ichi   = P[ichi]-1
        if i == (P[ichi] -1):
            break
# 出力
print(score_output) N,K = map(int,input().split()) N map(int,input().split()) map int input().split() ().split () input split K P =[] P [] C =[] C [] 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 score_output = -1000000000 score_output -1000000000 i = 0 i 0 for i in range(N):
    ichi = i
    score = 0
    step  = 0
    loop  = 0
    score_max = -1000000000
    while True:
        step  += 1
        score += C[ichi]
        ichi   = (P[ichi] -1)
        if i ==  (P[ichi] -1):
            score += C[ichi]
            break
        
    path = 0
    cnt  = 0
    
    while True:
        cnt  += 1
        path += C[ichi]
        
        if cnt > K:
            break
        
        loop = int((K - cnt) / (step+ 1))
        if score > 0:
            score_max = path + loop * score
        else:
            score_max = path
        if score_max > score_output:
            score_output = score_max

        ichi   = P[ichi]-1
        if i == (P[ichi] -1):
            break
# 出力 i range(N) range N ichi = i ichi i score = 0 score 0 step  = 0 step 0 loop  = 0 loop 0 score_max = -1000000000 score_max -1000000000 while True:
        step  += 1
        score += C[ichi]
        ichi   = (P[ichi] -1)
        if i ==  (P[ichi] -1):
            score += C[ichi]
            break
        
     True step  += 1 step 1 score += C[ichi] score C[ichi] C ichi ichi   = (P[ichi] -1) ichi P[ichi] -1 P[ichi] P ichi 1 if i ==  (P[ichi] -1):
            score += C[ichi]
            break
        
     i ==  (P[ichi] -1) i P[ichi] -1 P[ichi] P ichi 1 score += C[ichi] score C[ichi] C ichi break path = 0 path 0 cnt  = 0 cnt 0 while True:
        cnt  += 1
        path += C[ichi]
        
        if cnt > K:
            break
        
        loop = int((K - cnt) / (step+ 1))
        if score > 0:
            score_max = path + loop * score
        else:
            score_max = path
        if score_max > score_output:
            score_output = score_max

        ichi   = P[ichi]-1
        if i == (P[ichi] -1):
            break
# 出力 True cnt  += 1 cnt 1 path += C[ichi] path C[ichi] C ichi if cnt > K:
            break
        
         cnt > K cnt K break loop = int((K - cnt) / (step+ 1)) loop int((K - cnt) / (step+ 1)) int (K - cnt) / (step+ 1) K - cnt K cnt step+ 1 step 1 if score > 0:
            score_max = path + loop * score
        else:
            score_max = path
         score > 0 score 0 score_max = path + loop * score score_max path + loop * score path loop * score loop score score_max = path score_max path if score_max > score_output:
            score_output = score_max

         score_max > score_output score_max score_output score_output = score_max score_output score_max ichi   = P[ichi]-1 ichi P[ichi]-1 P[ichi] P ichi 1 if i == (P[ichi] -1):
            break
# 出力 i == (P[ichi] -1) i P[ichi] -1 P[ichi] P ichi 1 break print(score_output) print score_output loop = int((K - cnt) / (step+ 1)) int((K - cnt) / (step+ 1)) loop N,K = map(int,input().split()) map(int,input().split()) N P =[] [] P step  = 0 0 step ichi   = (P[ichi] -1) P[ichi] -1 ichi step  += 1 1 step C = list(map(int,input().split())) list(map(int,input().split())) C i = 0 0 i path = 0 0 path score = 0 0 score score += C[ichi] C[ichi] score score_output = -1000000000 -1000000000 score_output ichi = i i ichi score_max = -1000000000 -1000000000 score_max score_output = score_max score_max score_output loop  = 0 0 loop K = map(int,input().split()) map(int,input().split()) K P = list(map(int,input().split())) list(map(int,input().split())) P cnt  += 1 1 cnt score += C[ichi] C[ichi] score ichi   = P[ichi]-1 P[ichi]-1 ichi C =[] [] C score_max = path + loop * score path + loop * score score_max path += C[ichi] C[ichi] path score_max = path path score_max cnt  = 0 0 cnt
#D - Moving_piece
N,K = map(int,input().split())
P =[]
C =[]
P = list(map(int,input().split()))
C = list(map(int,input().split()))

score_output = -1000000000
i = 0
for i in range(N):
    ichi = i
    score = 0
    step  = 0
    loop  = 0
    score_max = -1000000000
    while True:
        step  += 1
        score += C[ichi]
        ichi   = (P[ichi] -1)
        if i ==  (P[ichi] -1):
            score += C[ichi]
            break
        
    path = 0
    cnt  = 0
    
    while True:
        cnt  += 1
        path += C[ichi]
        
        if cnt > K:
            break
        
        loop = int((K - cnt) / (step+ 1))
        if score > 0:
            score_max = path + loop * score
        else:
            score_max = path
        if score_max > score_output:
            score_output = score_max

        ichi   = P[ichi]-1
        if i == (P[ichi] -1):
            break
# 出力
print(score_output)
