import numpy as np
import numba
from numba import njit, b1, i4, i8, f8

@njit((i8,i8,i8[:],i8[:]), cache=True)
def main(N,K,P,C):
  INF = 1<<30
  ans = -INF
  for i in range(N):
    score = 0
    now = i
    visited = np.full(N,-1,np.int64)
    scores = np.zeros(N,np.int64)
    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

N, K = map(int, input().split())
P = np.array(list(map(lambda x:int(x)-1, input().split())))
C = np.array(list(map(int, input().split())))

print(main(N,K,P,C)) import numpy as np numpy import numba numba from numba import njit, b1, i4, i8, f8 def main(N,K,P,C):
  INF = 1<<30
  ans = -INF
  for i in range(N):
    score = 0
    now = i
    visited = np.full(N,-1,np.int64)
    scores = np.zeros(N,np.int64)
    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 main INF = 1<<30 INF 1<<30 1 30 ans = -INF ans -INF INF for i in range(N):
    score = 0
    now = i
    visited = np.full(N,-1,np.int64)
    scores = np.zeros(N,np.int64)
    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 = np.full(N,-1,np.int64) visited np.full(N,-1,np.int64) np.full np full N -1 np.int64 np int64 scores = np.zeros(N,np.int64) scores np.zeros(N,np.int64) np.zeros np zeros N np.int64 np int64 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 N N K K P P C C @njit((i8,i8,i8[:],i8[:]), cache=True) main (i8,i8,i8[:],i8[:]), cache=True @njit((i8,i8,i8[:],i8[:]), cache=True) njit (i8,i8,i8[:],i8[:]) i8 i8 i8[:] i8 : i8[:] i8 : cache=True cache True main N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K P = np.array(list(map(lambda x:int(x)-1, input().split()))) P np.array(list(map(lambda x:int(x)-1, input().split()))) np.array np array 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 = np.array(list(map(int, input().split()))) C np.array(list(map(int, input().split()))) np.array np array list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split print(main(N,K,P,C)) print main(N,K,P,C) main N K P C N, K = map(int, input().split()) map(int, input().split()) N P = np.array(list(map(lambda x:int(x)-1, input().split()))) np.array(list(map(lambda x:int(x)-1, input().split()))) P def main(N,K,P,C):
  INF = 1<<30
  ans = -INF
  for i in range(N):
    score = 0
    now = i
    visited = np.full(N,-1,np.int64)
    scores = np.zeros(N,np.int64)
    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 main(N,K,P,C):
  INF = 1<<30
  ans = -INF
  for i in range(N):
    score = 0
    now = i
    visited = np.full(N,-1,np.int64)
    scores = np.zeros(N,np.int64)
    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 main K = map(int, input().split()) map(int, input().split()) K C = np.array(list(map(int, input().split()))) np.array(list(map(int, input().split()))) C
import numpy as np
import numba
from numba import njit, b1, i4, i8, f8

@njit((i8,i8,i8[:],i8[:]), cache=True)
def main(N,K,P,C):
  INF = 1<<30
  ans = -INF
  for i in range(N):
    score = 0
    now = i
    visited = np.full(N,-1,np.int64)
    scores = np.zeros(N,np.int64)
    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

N, K = map(int, input().split())
P = np.array(list(map(lambda x:int(x)-1, input().split())))
C = np.array(list(map(int, input().split())))

print(main(N,K,P,C))