N, K = map(int, input().split())
moves = [0] + list(map(int, input().split()))
points = [0] + list(map(int, input().split()))
INF = 10**10
 
answer = -INF
for start in range(1, N+1):
  circle = [start]
  next = moves[start]
  c_score = points[start]
  while(next != start):
    circle.append(next)
    c_score += points[next]
    next = moves[next]
  m_score = 0
  for i in range(min(len(circle), K)):
    m_score += points[circle[i]]
    loop_cnt = (K-i-1)//len(circle)
    answer = max(answer, m_score + max(0, loop_cnt * c_score))
print(answer) N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K moves = [0] + list(map(int, input().split())) moves [0] + list(map(int, input().split())) [0] 0 list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split points = [0] + list(map(int, input().split())) points [0] + list(map(int, input().split())) [0] 0 list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split INF = 10**10 INF 10**10 10 10 answer = -INF answer -INF INF for start in range(1, N+1):
  circle = [start]
  next = moves[start]
  c_score = points[start]
  while(next != start):
    circle.append(next)
    c_score += points[next]
    next = moves[next]
  m_score = 0
  for i in range(min(len(circle), K)):
    m_score += points[circle[i]]
    loop_cnt = (K-i-1)//len(circle)
    answer = max(answer, m_score + max(0, loop_cnt * c_score)) start range(1, N+1) range 1 N+1 N 1 circle = [start] circle [start] start next = moves[start] next moves[start] moves start c_score = points[start] c_score points[start] points start while(next != start):
    circle.append(next)
    c_score += points[next]
    next = moves[next]
   next != start next start circle.append(next) circle.append circle append next c_score += points[next] c_score points[next] points next next = moves[next] next moves[next] moves next m_score = 0 m_score 0 for i in range(min(len(circle), K)):
    m_score += points[circle[i]]
    loop_cnt = (K-i-1)//len(circle)
    answer = max(answer, m_score + max(0, loop_cnt * c_score)) i range(min(len(circle), K)) range min(len(circle), K) min len(circle) len circle K m_score += points[circle[i]] m_score points[circle[i]] points circle[i] circle i loop_cnt = (K-i-1)//len(circle) loop_cnt (K-i-1)//len(circle) K-i-1 K-i K i 1 len(circle) len circle answer = max(answer, m_score + max(0, loop_cnt * c_score)) answer max(answer, m_score + max(0, loop_cnt * c_score)) max answer m_score + max(0, loop_cnt * c_score) m_score max(0, loop_cnt * c_score) max 0 loop_cnt * c_score loop_cnt c_score print(answer) print answer points = [0] + list(map(int, input().split())) [0] + list(map(int, input().split())) points answer = -INF -INF answer circle = [start] [start] circle c_score = points[start] points[start] c_score loop_cnt = (K-i-1)//len(circle) (K-i-1)//len(circle) loop_cnt c_score += points[next] points[next] c_score m_score += points[circle[i]] points[circle[i]] m_score next = moves[start] moves[start] next N, K = map(int, input().split()) map(int, input().split()) N answer = max(answer, m_score + max(0, loop_cnt * c_score)) max(answer, m_score + max(0, loop_cnt * c_score)) answer m_score = 0 0 m_score K = map(int, input().split()) map(int, input().split()) K next = moves[next] moves[next] next INF = 10**10 10**10 INF moves = [0] + list(map(int, input().split())) [0] + list(map(int, input().split())) moves
N, K = map(int, input().split())
moves = [0] + list(map(int, input().split()))
points = [0] + list(map(int, input().split()))
INF = 10**10
 
answer = -INF
for start in range(1, N+1):
  circle = [start]
  next = moves[start]
  c_score = points[start]
  while(next != start):
    circle.append(next)
    c_score += points[next]
    next = moves[next]
  m_score = 0
  for i in range(min(len(circle), K)):
    m_score += points[circle[i]]
    loop_cnt = (K-i-1)//len(circle)
    answer = max(answer, m_score + max(0, loop_cnt * c_score))
print(answer)