n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
p=[x-1 for x in p]
mi=set(range(n))
g={}
# g[id]=[ary]
while mi:
  x0=mi.pop()
  ary=[c[x0]]
  x=x0
  while p[x]!=x0:
    x=p[x]
    mi.discard(x)
    ary.append(c[x])
  cnt=len(ary)
  tmp=0
  ary*=2
  cary=[tmp]
  for x in ary:
    tmp+=x
    cary.append(tmp)
  g[x0]=[cnt,cary]
ans=-float('inf')
for cnt,cary in g.values():
  x,y=divmod(k,cnt)
  tmp1=max(0,cary[cnt]*x)
  tmp2=-float('inf')
  for i in range(cnt):
    for j in range(y):
      tmp2=max(tmp2,cary[i+j+1]-cary[i])
  ans=max(ans,tmp1+tmp2)
  if x:
    tmp1=max(0,cary[cnt]*(x-1))
    tmp2=-float('inf')
    for i in range(cnt):
      for j in range(cnt):
        tmp2=max(tmp2,cary[i+j+1]-cary[i])
    ans=max(ans,tmp1+tmp2)
  

print(ans) n,k=map(int,input().split()) n map(int,input().split()) map int input().split() ().split () input split k p=list(map(int,input().split())) p list(map(int,input().split())) list map(int,input().split()) map int 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 x-1 for x in p for x in p x p for x in p x-1 x 1 p=[x-1 for x in p] p [x-1 for x in p] mi=set(range(n)) mi set(range(n)) set range(n) range n g={} g {} while mi:
  x0=mi.pop()
  ary=[c[x0]]
  x=x0
  while p[x]!=x0:
    x=p[x]
    mi.discard(x)
    ary.append(c[x])
  cnt=len(ary)
  tmp=0
  ary*=2
  cary=[tmp]
  for x in ary:
    tmp+=x
    cary.append(tmp)
  g[x0]=[cnt,cary] mi x0=mi.pop() x0 mi.pop() mi.pop mi pop ary=[c[x0]] ary [c[x0]] c[x0] c x0 x=x0 x x0 while p[x]!=x0:
    x=p[x]
    mi.discard(x)
    ary.append(c[x])
   p[x]!=x0 p[x] p x x0 x=p[x] x p[x] p x mi.discard(x) mi.discard mi discard x ary.append(c[x]) ary.append ary append c[x] c x cnt=len(ary) cnt len(ary) len ary tmp=0 tmp 0 ary*=2 ary 2 cary=[tmp] cary [tmp] tmp for x in ary:
    tmp+=x
    cary.append(tmp)
   x ary tmp+=x tmp x cary.append(tmp) cary.append cary append tmp g[x0]=[cnt,cary] g[x0] g x0 [cnt,cary] cnt cary ans=-float('inf') ans -float('inf') float('inf') float 'inf' for cnt,cary in g.values():
  x,y=divmod(k,cnt)
  tmp1=max(0,cary[cnt]*x)
  tmp2=-float('inf')
  for i in range(cnt):
    for j in range(y):
      tmp2=max(tmp2,cary[i+j+1]-cary[i])
  ans=max(ans,tmp1+tmp2)
  if x:
    tmp1=max(0,cary[cnt]*(x-1))
    tmp2=-float('inf')
    for i in range(cnt):
      for j in range(cnt):
        tmp2=max(tmp2,cary[i+j+1]-cary[i])
    ans=max(ans,tmp1+tmp2)
   cnt cary g.values() g.values g values x,y=divmod(k,cnt) x divmod(k,cnt) divmod k cnt y tmp1=max(0,cary[cnt]*x) tmp1 max(0,cary[cnt]*x) max 0 cary[cnt]*x cary[cnt] cary cnt x tmp2=-float('inf') tmp2 -float('inf') float('inf') float 'inf' for i in range(cnt):
    for j in range(y):
      tmp2=max(tmp2,cary[i+j+1]-cary[i])
   i range(cnt) range cnt for j in range(y):
      tmp2=max(tmp2,cary[i+j+1]-cary[i])
   j range(y) range y tmp2=max(tmp2,cary[i+j+1]-cary[i]) tmp2 max(tmp2,cary[i+j+1]-cary[i]) max tmp2 cary[i+j+1]-cary[i] cary[i+j+1] cary i+j+1 i+j i j 1 cary[i] cary i ans=max(ans,tmp1+tmp2) ans max(ans,tmp1+tmp2) max ans tmp1+tmp2 tmp1 tmp2 if x:
    tmp1=max(0,cary[cnt]*(x-1))
    tmp2=-float('inf')
    for i in range(cnt):
      for j in range(cnt):
        tmp2=max(tmp2,cary[i+j+1]-cary[i])
    ans=max(ans,tmp1+tmp2)
   x tmp1=max(0,cary[cnt]*(x-1)) tmp1 max(0,cary[cnt]*(x-1)) max 0 cary[cnt]*(x-1) cary[cnt] cary cnt x-1 x 1 tmp2=-float('inf') tmp2 -float('inf') float('inf') float 'inf' for i in range(cnt):
      for j in range(cnt):
        tmp2=max(tmp2,cary[i+j+1]-cary[i])
     i range(cnt) range cnt for j in range(cnt):
        tmp2=max(tmp2,cary[i+j+1]-cary[i])
     j range(cnt) range cnt tmp2=max(tmp2,cary[i+j+1]-cary[i]) tmp2 max(tmp2,cary[i+j+1]-cary[i]) max tmp2 cary[i+j+1]-cary[i] cary[i+j+1] cary i+j+1 i+j i j 1 cary[i] cary i ans=max(ans,tmp1+tmp2) ans max(ans,tmp1+tmp2) max ans tmp1+tmp2 tmp1 tmp2 print(ans) print ans n,k=map(int,input().split()) map(int,input().split()) n p=[x-1 for x in p] [x-1 for x in p] p g={} {} g cary=[tmp] [tmp] cary ary=[c[x0]] [c[x0]] ary ans=max(ans,tmp1+tmp2) max(ans,tmp1+tmp2) ans ary*=2 2 ary mi=set(range(n)) set(range(n)) mi tmp2=-float('inf') -float('inf') tmp2 p=list(map(int,input().split())) list(map(int,input().split())) p c=list(map(int,input().split())) list(map(int,input().split())) c ans=-float('inf') -float('inf') ans k=map(int,input().split()) map(int,input().split()) k tmp2=max(tmp2,cary[i+j+1]-cary[i]) max(tmp2,cary[i+j+1]-cary[i]) tmp2 tmp+=x x tmp tmp2=max(tmp2,cary[i+j+1]-cary[i]) max(tmp2,cary[i+j+1]-cary[i]) tmp2 tmp1=max(0,cary[cnt]*x) max(0,cary[cnt]*x) tmp1 tmp2=-float('inf') -float('inf') tmp2 tmp1=max(0,cary[cnt]*(x-1)) max(0,cary[cnt]*(x-1)) tmp1 x=x0 x0 x ans=max(ans,tmp1+tmp2) max(ans,tmp1+tmp2) ans x0=mi.pop() mi.pop() x0 cnt=len(ary) len(ary) cnt y=divmod(k,cnt) divmod(k,cnt) y tmp=0 0 tmp x,y=divmod(k,cnt) divmod(k,cnt) x x=p[x] p[x] x
n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
p=[x-1 for x in p]
mi=set(range(n))
g={}
# g[id]=[ary]
while mi:
  x0=mi.pop()
  ary=[c[x0]]
  x=x0
  while p[x]!=x0:
    x=p[x]
    mi.discard(x)
    ary.append(c[x])
  cnt=len(ary)
  tmp=0
  ary*=2
  cary=[tmp]
  for x in ary:
    tmp+=x
    cary.append(tmp)
  g[x0]=[cnt,cary]
ans=-float('inf')
for cnt,cary in g.values():
  x,y=divmod(k,cnt)
  tmp1=max(0,cary[cnt]*x)
  tmp2=-float('inf')
  for i in range(cnt):
    for j in range(y):
      tmp2=max(tmp2,cary[i+j+1]-cary[i])
  ans=max(ans,tmp1+tmp2)
  if x:
    tmp1=max(0,cary[cnt]*(x-1))
    tmp2=-float('inf')
    for i in range(cnt):
      for j in range(cnt):
        tmp2=max(tmp2,cary[i+j+1]-cary[i])
    ans=max(ans,tmp1+tmp2)
  

print(ans)
