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

if K<=N:
  ans = -10**9
  for i in range(N):
    now,last = i,0
    for _ in range(K):
      last += C[P[now]]
      ans = max(ans,last)
      now=P[now]
  print(ans)
  exit()
ans = -10**9
for i in range(N):
  tmp,now,done = [0],i,set()
  for _ in range(N+1):
  #while now not in done:
    tmp.append(tmp[-1]+C[P[now]])
    if P[now] == i:
      break
    now = P[now]
  tmp = tmp[1:]
  cnt = len(tmp)
  m = max(tmp)
  ans = max(ans,m,tmp[-1]*(K//cnt-1)+m)
  if K%cnt != 0:
    ans = max(ans, tmp[-1]*(K//cnt)+max(tmp[:K%cnt]))
    #print(i,ans,tmp)
print(ans)

 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 if K<=N:
  ans = -10**9
  for i in range(N):
    now,last = i,0
    for _ in range(K):
      last += C[P[now]]
      ans = max(ans,last)
      now=P[now]
  print(ans)
  exit() K<=N K N ans = -10**9 ans -10**9 -10 9 for i in range(N):
    now,last = i,0
    for _ in range(K):
      last += C[P[now]]
      ans = max(ans,last)
      now=P[now]
   i range(N) range N now,last = i,0 now i last 0 for _ in range(K):
      last += C[P[now]]
      ans = max(ans,last)
      now=P[now]
   _ range(K) range K last += C[P[now]] last C[P[now]] C P[now] P now ans = max(ans,last) ans max(ans,last) max ans last now=P[now] now P[now] P now print(ans) print ans exit() exit ans = -10**9 ans -10**9 -10 9 for i in range(N):
  tmp,now,done = [0],i,set()
  for _ in range(N+1):
  #while now not in done:
    tmp.append(tmp[-1]+C[P[now]])
    if P[now] == i:
      break
    now = P[now]
  tmp = tmp[1:]
  cnt = len(tmp)
  m = max(tmp)
  ans = max(ans,m,tmp[-1]*(K//cnt-1)+m)
  if K%cnt != 0:
    ans = max(ans, tmp[-1]*(K//cnt)+max(tmp[:K%cnt]))
    #print(i,ans,tmp) i range(N) range N tmp,now,done = [0],i,set() tmp [0] 0 now i done set() set for _ in range(N+1):
  #while now not in done:
    tmp.append(tmp[-1]+C[P[now]])
    if P[now] == i:
      break
    now = P[now]
   _ range(N+1) range N+1 N 1 tmp.append(tmp[-1]+C[P[now]]) tmp.append tmp append tmp[-1]+C[P[now]] tmp[-1] tmp -1 C[P[now]] C P[now] P now if P[now] == i:
      break
     P[now] == i P[now] P now i break now = P[now] now P[now] P now tmp = tmp[1:] tmp tmp[1:] tmp 1: 1 cnt = len(tmp) cnt len(tmp) len tmp m = max(tmp) m max(tmp) max tmp ans = max(ans,m,tmp[-1]*(K//cnt-1)+m) ans max(ans,m,tmp[-1]*(K//cnt-1)+m) max ans m tmp[-1]*(K//cnt-1)+m tmp[-1]*(K//cnt-1) tmp[-1] tmp -1 K//cnt-1 K//cnt K cnt 1 m if K%cnt != 0:
    ans = max(ans, tmp[-1]*(K//cnt)+max(tmp[:K%cnt]))
    #print(i,ans,tmp) K%cnt != 0 K%cnt K cnt 0 ans = max(ans, tmp[-1]*(K//cnt)+max(tmp[:K%cnt])) ans max(ans, tmp[-1]*(K//cnt)+max(tmp[:K%cnt])) max ans tmp[-1]*(K//cnt)+max(tmp[:K%cnt]) tmp[-1]*(K//cnt) tmp[-1] tmp -1 K//cnt K cnt max(tmp[:K%cnt]) max tmp[:K%cnt] tmp :K%cnt K%cnt K cnt print(ans) print ans done = [0],i,set() set() done last += C[P[now]] C[P[now]] last C=list(map(int,input().split())) list(map(int,input().split())) C ans = max(ans,last) max(ans,last) ans N,K=map(int,input().split()) map(int,input().split()) N tmp,now,done = [0],i,set() [0] tmp cnt = len(tmp) len(tmp) cnt ans = -10**9 -10**9 ans tmp = tmp[1:] tmp[1:] tmp m = max(tmp) max(tmp) m ans = -10**9 -10**9 ans ans = max(ans, tmp[-1]*(K//cnt)+max(tmp[:K%cnt])) max(ans, tmp[-1]*(K//cnt)+max(tmp[:K%cnt])) ans now = P[now] P[now] now now,last = i,0 i now now,done = [0],i,set() i now P=list(map(lambda x:int(x)-1,input().split())) list(map(lambda x:int(x)-1,input().split())) P now=P[now] P[now] now K=map(int,input().split()) map(int,input().split()) K last = i,0 0 last ans = max(ans,m,tmp[-1]*(K//cnt-1)+m) max(ans,m,tmp[-1]*(K//cnt-1)+m) ans
N,K=map(int,input().split())
P=list(map(lambda x:int(x)-1,input().split()))
C=list(map(int,input().split()))

if K<=N:
  ans = -10**9
  for i in range(N):
    now,last = i,0
    for _ in range(K):
      last += C[P[now]]
      ans = max(ans,last)
      now=P[now]
  print(ans)
  exit()
ans = -10**9
for i in range(N):
  tmp,now,done = [0],i,set()
  for _ in range(N+1):
  #while now not in done:
    tmp.append(tmp[-1]+C[P[now]])
    if P[now] == i:
      break
    now = P[now]
  tmp = tmp[1:]
  cnt = len(tmp)
  m = max(tmp)
  ans = max(ans,m,tmp[-1]*(K//cnt-1)+m)
  if K%cnt != 0:
    ans = max(ans, tmp[-1]*(K//cnt)+max(tmp[:K%cnt]))
    #print(i,ans,tmp)
print(ans)


