N,K=map(int,input().split())
P=[int(x)-1 for x in input().split()]
C=[int(x) for x in input().split()]

def circle_grouping(P):
  P_in=[]
  P_group=[]
  for i in range(N):
    if i in P_in:
      pass
    else:
      start=i
      j=P[i]
      ls=[i]
      P_in.append(i)
      while j!=start:
        ls.append(j)
        P_in.append(j)
        j=P[j]
      P_group.append(ls)
  return P_group

def circle_maximum(A,K):
  max_val=max(A)
  for i in range(len(A)):
    val=0
    for k in range(K):
      pos=(i+k)%len(A)
      val+=A[pos]
      max_val=max(max_val,val)
  return max_val

score=max(C)
P_group=circle_grouping(P)
for A in P_group:
  k_rem=K%len(A)
  cycle=K//len(A)
  if cycle>=1:
    k_rem+=len(A)
    cycle-=1
  B=[C[i] for i in A]
  score=max(score,max(0,sum(B))*cycle+circle_maximum(B,k_rem))
print(score) N,K=map(int,input().split()) N map(int,input().split()) map int input().split() ().split () input split K int(x)-1 for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x)-1 int(x) int x 1 P=[int(x)-1 for x in input().split()] P [int(x)-1 for x in input().split()] int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x C=[int(x) for x in input().split()] C [int(x) for x in input().split()] def circle_grouping(P):
  P_in=[]
  P_group=[]
  for i in range(N):
    if i in P_in:
      pass
    else:
      start=i
      j=P[i]
      ls=[i]
      P_in.append(i)
      while j!=start:
        ls.append(j)
        P_in.append(j)
        j=P[j]
      P_group.append(ls)
  return P_group circle_grouping P_in=[] P_in [] P_group=[] P_group [] for i in range(N):
    if i in P_in:
      pass
    else:
      start=i
      j=P[i]
      ls=[i]
      P_in.append(i)
      while j!=start:
        ls.append(j)
        P_in.append(j)
        j=P[j]
      P_group.append(ls)
   i range(N) range N if i in P_in:
      pass
    else:
      start=i
      j=P[i]
      ls=[i]
      P_in.append(i)
      while j!=start:
        ls.append(j)
        P_in.append(j)
        j=P[j]
      P_group.append(ls)
   i in P_in i P_in start=i start i j=P[i] j P[i] P i ls=[i] ls [i] i P_in.append(i) P_in.append P_in append i while j!=start:
        ls.append(j)
        P_in.append(j)
        j=P[j]
       j!=start j start ls.append(j) ls.append ls append j P_in.append(j) P_in.append P_in append j j=P[j] j P[j] P j P_group.append(ls) P_group.append P_group append ls return P_group P_group P P def circle_maximum(A,K):
  max_val=max(A)
  for i in range(len(A)):
    val=0
    for k in range(K):
      pos=(i+k)%len(A)
      val+=A[pos]
      max_val=max(max_val,val)
  return max_val circle_maximum max_val=max(A) max_val max(A) max A for i in range(len(A)):
    val=0
    for k in range(K):
      pos=(i+k)%len(A)
      val+=A[pos]
      max_val=max(max_val,val)
   i range(len(A)) range len(A) len A val=0 val 0 for k in range(K):
      pos=(i+k)%len(A)
      val+=A[pos]
      max_val=max(max_val,val)
   k range(K) range K pos=(i+k)%len(A) pos (i+k)%len(A) i+k i k len(A) len A val+=A[pos] val A[pos] A pos max_val=max(max_val,val) max_val max(max_val,val) max max_val val return max_val max_val A A K K score=max(C) score max(C) max C P_group=circle_grouping(P) P_group circle_grouping(P) circle_grouping P for A in P_group:
  k_rem=K%len(A)
  cycle=K//len(A)
  if cycle>=1:
    k_rem+=len(A)
    cycle-=1
  B=[C[i] for i in A]
  score=max(score,max(0,sum(B))*cycle+circle_maximum(B,k_rem)) A P_group k_rem=K%len(A) k_rem K%len(A) K len(A) len A cycle=K//len(A) cycle K//len(A) K len(A) len A if cycle>=1:
    k_rem+=len(A)
    cycle-=1
   cycle>=1 cycle 1 k_rem+=len(A) k_rem len(A) len A cycle-=1 cycle 1 C[i] for i in A for i in A i A for i in A C[i] C i B=[C[i] for i in A] B [C[i] for i in A] score=max(score,max(0,sum(B))*cycle+circle_maximum(B,k_rem)) score max(score,max(0,sum(B))*cycle+circle_maximum(B,k_rem)) max score max(0,sum(B))*cycle+circle_maximum(B,k_rem) max(0,sum(B))*cycle max(0,sum(B)) max 0 sum(B) sum B cycle circle_maximum(B,k_rem) circle_maximum B k_rem print(score) print score K=map(int,input().split()) map(int,input().split()) K N,K=map(int,input().split()) map(int,input().split()) N P_group=circle_grouping(P) circle_grouping(P) P_group cycle-=1 1 cycle P=[int(x)-1 for x in input().split()] [int(x)-1 for x in input().split()] P score=max(C) max(C) score B=[C[i] for i in A] [C[i] for i in A] B cycle=K//len(A) K//len(A) cycle def circle_grouping(P):
  P_in=[]
  P_group=[]
  for i in range(N):
    if i in P_in:
      pass
    else:
      start=i
      j=P[i]
      ls=[i]
      P_in.append(i)
      while j!=start:
        ls.append(j)
        P_in.append(j)
        j=P[j]
      P_group.append(ls)
  return P_group def circle_grouping(P):
  P_in=[]
  P_group=[]
  for i in range(N):
    if i in P_in:
      pass
    else:
      start=i
      j=P[i]
      ls=[i]
      P_in.append(i)
      while j!=start:
        ls.append(j)
        P_in.append(j)
        j=P[j]
      P_group.append(ls)
  return P_group circle_grouping def circle_maximum(A,K):
  max_val=max(A)
  for i in range(len(A)):
    val=0
    for k in range(K):
      pos=(i+k)%len(A)
      val+=A[pos]
      max_val=max(max_val,val)
  return max_val def circle_maximum(A,K):
  max_val=max(A)
  for i in range(len(A)):
    val=0
    for k in range(K):
      pos=(i+k)%len(A)
      val+=A[pos]
      max_val=max(max_val,val)
  return max_val circle_maximum k_rem+=len(A) len(A) k_rem C=[int(x) for x in input().split()] [int(x) for x in input().split()] C score=max(score,max(0,sum(B))*cycle+circle_maximum(B,k_rem)) max(score,max(0,sum(B))*cycle+circle_maximum(B,k_rem)) score k_rem=K%len(A) K%len(A) k_rem
N,K=map(int,input().split())
P=[int(x)-1 for x in input().split()]
C=[int(x) for x in input().split()]

def circle_grouping(P):
  P_in=[]
  P_group=[]
  for i in range(N):
    if i in P_in:
      pass
    else:
      start=i
      j=P[i]
      ls=[i]
      P_in.append(i)
      while j!=start:
        ls.append(j)
        P_in.append(j)
        j=P[j]
      P_group.append(ls)
  return P_group

def circle_maximum(A,K):
  max_val=max(A)
  for i in range(len(A)):
    val=0
    for k in range(K):
      pos=(i+k)%len(A)
      val+=A[pos]
      max_val=max(max_val,val)
  return max_val

score=max(C)
P_group=circle_grouping(P)
for A in P_group:
  k_rem=K%len(A)
  cycle=K//len(A)
  if cycle>=1:
    k_rem+=len(A)
    cycle-=1
  B=[C[i] for i in A]
  score=max(score,max(0,sum(B))*cycle+circle_maximum(B,k_rem))
print(score)