# -*- coding: utf-8 -*-
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
k_max = 10100
score = [-10**18-1] * N

for i in range(N):
  score_i = [-10**18-1] * k_max # iからスタートしてK回操作する場合の累積和
  score_i[0] = C[P[i]-1]
  index = P[i]-1
  for k in range(1, K):
    if index == i:
      # 既に進んだことがあるマスの場合
      
      loop_score = score_i[k-1] # 1ループで増えるスコア
      if loop_score > 0:
        loop_num = k # ループする要素数はk(indexが0 ~ k-1)
        
        tmp_score = [-10**18-1] * k_max
        tmp_score[0] = (K//k -1) * loop_score
#        print("loop_score", loop_score)
#        print("tmp_score", tmp_score)
        tmp_index = index
#        print("loop_num + K%k + 1", loop_num + K%k + 1)
        for j in range (1, loop_num + K%k + 1):
          tmp_score[j] = tmp_score[j-1] + C[P[tmp_index]-1]
          tmp_index = P[tmp_index]-1
#        print("max(tmp_score)", max(tmp_score))
        score_i[k] = max(tmp_score)
      break
    score_i[k] = score_i[k-1] + C[P[index]-1]
    index = P[index]-1
  score[i] = max(score_i)
print(max(score)) 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_max = 10100 k_max 10100 score = [-10**18-1] * N score [-10**18-1] * N [-10**18-1] -10**18-1 -10**18 -10 18 1 N for i in range(N):
  score_i = [-10**18-1] * k_max # iからスタートしてK回操作する場合の累積和
  score_i[0] = C[P[i]-1]
  index = P[i]-1
  for k in range(1, K):
    if index == i:
      # 既に進んだことがあるマスの場合
      
      loop_score = score_i[k-1] # 1ループで増えるスコア
      if loop_score > 0:
        loop_num = k # ループする要素数はk(indexが0 ~ k-1)
        
        tmp_score = [-10**18-1] * k_max
        tmp_score[0] = (K//k -1) * loop_score
#        print("loop_score", loop_score)
#        print("tmp_score", tmp_score)
        tmp_index = index
#        print("loop_num + K%k + 1", loop_num + K%k + 1)
        for j in range (1, loop_num + K%k + 1):
          tmp_score[j] = tmp_score[j-1] + C[P[tmp_index]-1]
          tmp_index = P[tmp_index]-1
#        print("max(tmp_score)", max(tmp_score))
        score_i[k] = max(tmp_score)
      break
    score_i[k] = score_i[k-1] + C[P[index]-1]
    index = P[index]-1
  score[i] = max(score_i) i range(N) range N score_i = [-10**18-1] * k_max score_i [-10**18-1] * k_max [-10**18-1] -10**18-1 -10**18 -10 18 1 k_max score_i[0] = C[P[i]-1] score_i[0] score_i 0 C[P[i]-1] C P[i]-1 P[i] P i 1 index = P[i]-1 index P[i]-1 P[i] P i 1 for k in range(1, K):
    if index == i:
      # 既に進んだことがあるマスの場合
      
      loop_score = score_i[k-1] # 1ループで増えるスコア
      if loop_score > 0:
        loop_num = k # ループする要素数はk(indexが0 ~ k-1)
        
        tmp_score = [-10**18-1] * k_max
        tmp_score[0] = (K//k -1) * loop_score
#        print("loop_score", loop_score)
#        print("tmp_score", tmp_score)
        tmp_index = index
#        print("loop_num + K%k + 1", loop_num + K%k + 1)
        for j in range (1, loop_num + K%k + 1):
          tmp_score[j] = tmp_score[j-1] + C[P[tmp_index]-1]
          tmp_index = P[tmp_index]-1
#        print("max(tmp_score)", max(tmp_score))
        score_i[k] = max(tmp_score)
      break
    score_i[k] = score_i[k-1] + C[P[index]-1]
    index = P[index]-1
   k range(1, K) range 1 K if index == i:
      # 既に進んだことがあるマスの場合
      
      loop_score = score_i[k-1] # 1ループで増えるスコア
      if loop_score > 0:
        loop_num = k # ループする要素数はk(indexが0 ~ k-1)
        
        tmp_score = [-10**18-1] * k_max
        tmp_score[0] = (K//k -1) * loop_score
#        print("loop_score", loop_score)
#        print("tmp_score", tmp_score)
        tmp_index = index
#        print("loop_num + K%k + 1", loop_num + K%k + 1)
        for j in range (1, loop_num + K%k + 1):
          tmp_score[j] = tmp_score[j-1] + C[P[tmp_index]-1]
          tmp_index = P[tmp_index]-1
#        print("max(tmp_score)", max(tmp_score))
        score_i[k] = max(tmp_score)
      break
     index == i index i loop_score = score_i[k-1] loop_score score_i[k-1] score_i k-1 k 1 if loop_score > 0:
        loop_num = k # ループする要素数はk(indexが0 ~ k-1)
        
        tmp_score = [-10**18-1] * k_max
        tmp_score[0] = (K//k -1) * loop_score
#        print("loop_score", loop_score)
#        print("tmp_score", tmp_score)
        tmp_index = index
#        print("loop_num + K%k + 1", loop_num + K%k + 1)
        for j in range (1, loop_num + K%k + 1):
          tmp_score[j] = tmp_score[j-1] + C[P[tmp_index]-1]
          tmp_index = P[tmp_index]-1
#        print("max(tmp_score)", max(tmp_score))
        score_i[k] = max(tmp_score)
       loop_score > 0 loop_score 0 loop_num = k loop_num k tmp_score = [-10**18-1] * k_max tmp_score [-10**18-1] * k_max [-10**18-1] -10**18-1 -10**18 -10 18 1 k_max tmp_score[0] = (K//k -1) * loop_score tmp_score[0] tmp_score 0 (K//k -1) * loop_score K//k -1 K//k K k 1 loop_score tmp_index = index tmp_index index for j in range (1, loop_num + K%k + 1):
          tmp_score[j] = tmp_score[j-1] + C[P[tmp_index]-1]
          tmp_index = P[tmp_index]-1
#        print("max(tmp_score)", max(tmp_score))
         j range (1, loop_num + K%k + 1) range 1 loop_num + K%k + 1 loop_num + K%k loop_num K%k K k 1 tmp_score[j] = tmp_score[j-1] + C[P[tmp_index]-1] tmp_score[j] tmp_score j tmp_score[j-1] + C[P[tmp_index]-1] tmp_score[j-1] tmp_score j-1 j 1 C[P[tmp_index]-1] C P[tmp_index]-1 P[tmp_index] P tmp_index 1 tmp_index = P[tmp_index]-1 tmp_index P[tmp_index]-1 P[tmp_index] P tmp_index 1 score_i[k] = max(tmp_score) score_i[k] score_i k max(tmp_score) max tmp_score break score_i[k] = score_i[k-1] + C[P[index]-1] score_i[k] score_i k score_i[k-1] + C[P[index]-1] score_i[k-1] score_i k-1 k 1 C[P[index]-1] C P[index]-1 P[index] P index 1 index = P[index]-1 index P[index]-1 P[index] P index 1 score[i] = max(score_i) score[i] score i max(score_i) max score_i print(max(score)) print max(score) max score index = P[i]-1 P[i]-1 index K = map(int, input().split()) map(int, input().split()) K index = P[index]-1 P[index]-1 index k_max = 10100 10100 k_max loop_num = k k loop_num tmp_index = index index tmp_index N, K = map(int, input().split()) map(int, input().split()) N loop_score = score_i[k-1] score_i[k-1] loop_score P = list(map(int, input().split())) list(map(int, input().split())) P score = [-10**18-1] * N [-10**18-1] * N score tmp_index = P[tmp_index]-1 P[tmp_index]-1 tmp_index score_i = [-10**18-1] * k_max [-10**18-1] * k_max score_i tmp_score = [-10**18-1] * k_max [-10**18-1] * k_max tmp_score C = list(map(int, input().split())) list(map(int, input().split())) C
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
k_max = 10100
score = [-10**18-1] * N

for i in range(N):
  score_i = [-10**18-1] * k_max # iからスタートしてK回操作する場合の累積和
  score_i[0] = C[P[i]-1]
  index = P[i]-1
  for k in range(1, K):
    if index == i:
      # 既に進んだことがあるマスの場合
      
      loop_score = score_i[k-1] # 1ループで増えるスコア
      if loop_score > 0:
        loop_num = k # ループする要素数はk(indexが0 ~ k-1)
        
        tmp_score = [-10**18-1] * k_max
        tmp_score[0] = (K//k -1) * loop_score
#        print("loop_score", loop_score)
#        print("tmp_score", tmp_score)
        tmp_index = index
#        print("loop_num + K%k + 1", loop_num + K%k + 1)
        for j in range (1, loop_num + K%k + 1):
          tmp_score[j] = tmp_score[j-1] + C[P[tmp_index]-1]
          tmp_index = P[tmp_index]-1
#        print("max(tmp_score)", max(tmp_score))
        score_i[k] = max(tmp_score)
      break
    score_i[k] = score_i[k-1] + C[P[index]-1]
    index = P[index]-1
  score[i] = max(score_i)
print(max(score))