def solve():
  ans = -float('inf')
  N, K = map(int, input().split())
  P = list(map(lambda x:int(x)-1, input().split()))
  C = list(map(int, input().split()))
  for i in range(N):
    score = 0
    now = i
    visited = [-1]*N
    scores = [0]*N
    visited[now] = 0
    for j in range(1,K+1):
      now = P[now]
      score += C[now]
      ans = max(ans, score)
      if visited[now]>=0:
        cyc = j - visited[now]
        up = score - scores[now]
        break
      scores[now] = score
      visited[now] = j
    else:
      continue
    if up<=0:
      continue
    cnt = j+1
    if K-cyc>=j:
      score += (K-cyc-j)//cyc * up
      ans = max(ans, score)
      cnt += (K-cyc-j)//cyc*cyc
    for j in range(cnt,K+1):
      now = P[now]
      score += C[now]
      ans = max(ans, score)
  return ans
print(solve()) def solve():
  ans = -float('inf')
  N, K = map(int, input().split())
  P = list(map(lambda x:int(x)-1, input().split()))
  C = list(map(int, input().split()))
  for i in range(N):
    score = 0
    now = i
    visited = [-1]*N
    scores = [0]*N
    visited[now] = 0
    for j in range(1,K+1):
      now = P[now]
      score += C[now]
      ans = max(ans, score)
      if visited[now]>=0:
        cyc = j - visited[now]
        up = score - scores[now]
        break
      scores[now] = score
      visited[now] = j
    else:
      continue
    if up<=0:
      continue
    cnt = j+1
    if K-cyc>=j:
      score += (K-cyc-j)//cyc * up
      ans = max(ans, score)
      cnt += (K-cyc-j)//cyc*cyc
    for j in range(cnt,K+1):
      now = P[now]
      score += C[now]
      ans = max(ans, score)
  return ans solve ans = -float('inf') ans -float('inf') float('inf') float 'inf' N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K P = list(map(lambda x:int(x)-1, input().split())) P list(map(lambda x:int(x)-1, input().split())) list map(lambda x:int(x)-1, input().split()) map lambda x:int(x)-1 int(x)-1 int(x) int x 1 x 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 for i in range(N):
    score = 0
    now = i
    visited = [-1]*N
    scores = [0]*N
    visited[now] = 0
    for j in range(1,K+1):
      now = P[now]
      score += C[now]
      ans = max(ans, score)
      if visited[now]>=0:
        cyc = j - visited[now]
        up = score - scores[now]
        break
      scores[now] = score
      visited[now] = j
    else:
      continue
    if up<=0:
      continue
    cnt = j+1
    if K-cyc>=j:
      score += (K-cyc-j)//cyc * up
      ans = max(ans, score)
      cnt += (K-cyc-j)//cyc*cyc
    for j in range(cnt,K+1):
      now = P[now]
      score += C[now]
      ans = max(ans, score)
   i range(N) range N score = 0 score 0 now = i now i visited = [-1]*N visited [-1]*N [-1] -1 N scores = [0]*N scores [0]*N [0] 0 N visited[now] = 0 visited[now] visited now 0 for j in range(1,K+1):
      now = P[now]
      score += C[now]
      ans = max(ans, score)
      if visited[now]>=0:
        cyc = j - visited[now]
        up = score - scores[now]
        break
      scores[now] = score
      visited[now] = j
    else:
      continue
     j range(1,K+1) range 1 K+1 K 1 now = P[now] now P[now] P now score += C[now] score C[now] C now ans = max(ans, score) ans max(ans, score) max ans score if visited[now]>=0:
        cyc = j - visited[now]
        up = score - scores[now]
        break
       visited[now]>=0 visited[now] visited now 0 cyc = j - visited[now] cyc j - visited[now] j visited[now] visited now up = score - scores[now] up score - scores[now] score scores[now] scores now break scores[now] = score scores[now] scores now score visited[now] = j visited[now] visited now j continue if up<=0:
      continue
     up<=0 up 0 continue cnt = j+1 cnt j+1 j 1 if K-cyc>=j:
      score += (K-cyc-j)//cyc * up
      ans = max(ans, score)
      cnt += (K-cyc-j)//cyc*cyc
     K-cyc>=j K-cyc K cyc j score += (K-cyc-j)//cyc * up score (K-cyc-j)//cyc * up (K-cyc-j)//cyc K-cyc-j K-cyc K cyc j cyc up ans = max(ans, score) ans max(ans, score) max ans score cnt += (K-cyc-j)//cyc*cyc cnt (K-cyc-j)//cyc*cyc (K-cyc-j)//cyc K-cyc-j K-cyc K cyc j cyc cyc for j in range(cnt,K+1):
      now = P[now]
      score += C[now]
      ans = max(ans, score)
   j range(cnt,K+1) range cnt K+1 K 1 now = P[now] now P[now] P now score += C[now] score C[now] C now ans = max(ans, score) ans max(ans, score) max ans score return ans ans print(solve()) print solve() solve def solve():
  ans = -float('inf')
  N, K = map(int, input().split())
  P = list(map(lambda x:int(x)-1, input().split()))
  C = list(map(int, input().split()))
  for i in range(N):
    score = 0
    now = i
    visited = [-1]*N
    scores = [0]*N
    visited[now] = 0
    for j in range(1,K+1):
      now = P[now]
      score += C[now]
      ans = max(ans, score)
      if visited[now]>=0:
        cyc = j - visited[now]
        up = score - scores[now]
        break
      scores[now] = score
      visited[now] = j
    else:
      continue
    if up<=0:
      continue
    cnt = j+1
    if K-cyc>=j:
      score += (K-cyc-j)//cyc * up
      ans = max(ans, score)
      cnt += (K-cyc-j)//cyc*cyc
    for j in range(cnt,K+1):
      now = P[now]
      score += C[now]
      ans = max(ans, score)
  return ans def solve():
  ans = -float('inf')
  N, K = map(int, input().split())
  P = list(map(lambda x:int(x)-1, input().split()))
  C = list(map(int, input().split()))
  for i in range(N):
    score = 0
    now = i
    visited = [-1]*N
    scores = [0]*N
    visited[now] = 0
    for j in range(1,K+1):
      now = P[now]
      score += C[now]
      ans = max(ans, score)
      if visited[now]>=0:
        cyc = j - visited[now]
        up = score - scores[now]
        break
      scores[now] = score
      visited[now] = j
    else:
      continue
    if up<=0:
      continue
    cnt = j+1
    if K-cyc>=j:
      score += (K-cyc-j)//cyc * up
      ans = max(ans, score)
      cnt += (K-cyc-j)//cyc*cyc
    for j in range(cnt,K+1):
      now = P[now]
      score += C[now]
      ans = max(ans, score)
  return ans solve
def solve():
  ans = -float('inf')
  N, K = map(int, input().split())
  P = list(map(lambda x:int(x)-1, input().split()))
  C = list(map(int, input().split()))
  for i in range(N):
    score = 0
    now = i
    visited = [-1]*N
    scores = [0]*N
    visited[now] = 0
    for j in range(1,K+1):
      now = P[now]
      score += C[now]
      ans = max(ans, score)
      if visited[now]>=0:
        cyc = j - visited[now]
        up = score - scores[now]
        break
      scores[now] = score
      visited[now] = j
    else:
      continue
    if up<=0:
      continue
    cnt = j+1
    if K-cyc>=j:
      score += (K-cyc-j)//cyc * up
      ans = max(ans, score)
      cnt += (K-cyc-j)//cyc*cyc
    for j in range(cnt,K+1):
      now = P[now]
      score += C[now]
      ans = max(ans, score)
  return ans
print(solve())