N, K = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))
K = K-1
ans = -10**9-1
for h in range(N):
   i = h+1
   start = i
   circle = [c[i-1]]
   i = p[i-1]
   while i != start:
      score = circle[-1] + c[i-1]
      circle.append(score)
      i = p[i-1]
   l=len(circle)
   for j in range(min(l, K+1)):
      if circle[l-1] > 0:
         score = circle[j] + ((K - j)//l)*circle[l-1]
      else:
         score = circle[j]
      ans = max(score, 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 K = K-1 K K-1 K 1 ans = -10**9-1 ans -10**9-1 -10**9 -10 9 1 for h in range(N):
   i = h+1
   start = i
   circle = [c[i-1]]
   i = p[i-1]
   while i != start:
      score = circle[-1] + c[i-1]
      circle.append(score)
      i = p[i-1]
   l=len(circle)
   for j in range(min(l, K+1)):
      if circle[l-1] > 0:
         score = circle[j] + ((K - j)//l)*circle[l-1]
      else:
         score = circle[j]
      ans = max(score, ans)       h range(N) range N i = h+1 i h+1 h 1 start = i start i circle = [c[i-1]] circle [c[i-1]] c[i-1] c i-1 i 1 i = p[i-1] i p[i-1] p i-1 i 1 while i != start:
      score = circle[-1] + c[i-1]
      circle.append(score)
      i = p[i-1]
    i != start i start score = circle[-1] + c[i-1] score circle[-1] + c[i-1] circle[-1] circle -1 c[i-1] c i-1 i 1 circle.append(score) circle.append circle append score i = p[i-1] i p[i-1] p i-1 i 1 l=len(circle) l len(circle) len circle for j in range(min(l, K+1)):
      if circle[l-1] > 0:
         score = circle[j] + ((K - j)//l)*circle[l-1]
      else:
         score = circle[j]
      ans = max(score, ans)       j range(min(l, K+1)) range min(l, K+1) min l K+1 K 1 if circle[l-1] > 0:
         score = circle[j] + ((K - j)//l)*circle[l-1]
      else:
         score = circle[j]
       circle[l-1] > 0 circle[l-1] circle l-1 l 1 0 score = circle[j] + ((K - j)//l)*circle[l-1] score circle[j] + ((K - j)//l)*circle[l-1] circle[j] circle j ((K - j)//l)*circle[l-1] (K - j)//l K - j K j l circle[l-1] circle l-1 l 1 score = circle[j] score circle[j] circle j ans = max(score, ans) ans max(score, ans) max score ans print(ans) print ans K = map(int,input().split()) map(int,input().split()) K start = i i start K = K-1 K-1 K i = h+1 h+1 i ans = max(score, ans) max(score, ans) ans l=len(circle) len(circle) l i = p[i-1] p[i-1] i score = circle[j] + ((K - j)//l)*circle[l-1] circle[j] + ((K - j)//l)*circle[l-1] score c = list(map(int,input().split())) list(map(int,input().split())) c p = list(map(int,input().split())) list(map(int,input().split())) p score = circle[j] circle[j] score i = p[i-1] p[i-1] i score = circle[-1] + c[i-1] circle[-1] + c[i-1] score N, K = map(int,input().split()) map(int,input().split()) N ans = -10**9-1 -10**9-1 ans circle = [c[i-1]] [c[i-1]] circle
N, K = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))
K = K-1
ans = -10**9-1
for h in range(N):
   i = h+1
   start = i
   circle = [c[i-1]]
   i = p[i-1]
   while i != start:
      score = circle[-1] + c[i-1]
      circle.append(score)
      i = p[i-1]
   l=len(circle)
   for j in range(min(l, K+1)):
      if circle[l-1] > 0:
         score = circle[j] + ((K - j)//l)*circle[l-1]
      else:
         score = circle[j]
      ans = max(score, ans)      
print(ans)