INF=10**9
f=lambda:[*map(int,input().split())]
n,k=f()
p,c=f(),f()
p=[*map(lambda x:x-1,p)]
tp=[[*range(n)] for _ in range(n+1)]
tc=[[0]*n for _ in range(n+1)]
lt=[0]*n
lc=[0]*n
for i in range(n):
  for j in range(n):
    t=p[tp[i][j]]
    tp[i+1][j]=t
    tc[i+1][j]=tc[i][j]+c[t]
    if t==j and lt[j]==0:
      lt[j]=i+1
      lc[j]=tc[i+1][j]
a=-INF
for i in range(n):
  if lc[i]<1 or k<=lt[i]:
    for j in range(min(lt[i],k)):
      a=max(a,tc[j+1][i])
  else:
    for j in range(lt[i]):
      t=k//lt[i]*lc[i]+tc[j+1][i]
      if k%lt[i]<j+1: t-=lc[i]
      a=max(a,t)
print(a) INF=10**9 INF 10**9 10 9 f=lambda:[*map(int,input().split())] f lambda:[*map(int,input().split())] [*map(int,input().split())] *map(int,input().split()) map(int,input().split()) map int input().split() ().split () input split n,k=f() n f() f k p,c=f(),f() p f() f c f() f p=[*map(lambda x:x-1,p)] p [*map(lambda x:x-1,p)] *map(lambda x:x-1,p) map(lambda x:x-1,p) map lambda x:x-1 x-1 x 1 x p [*range(n)] for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) [*range(n)] *range(n) range(n) range n tp=[[*range(n)] for _ in range(n+1)] tp [[*range(n)] for _ in range(n+1)] [0]*n for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) [0]*n [0] 0 n tc=[[0]*n for _ in range(n+1)] tc [[0]*n for _ in range(n+1)] lt=[0]*n lt [0]*n [0] 0 n lc=[0]*n lc [0]*n [0] 0 n for i in range(n):
  for j in range(n):
    t=p[tp[i][j]]
    tp[i+1][j]=t
    tc[i+1][j]=tc[i][j]+c[t]
    if t==j and lt[j]==0:
      lt[j]=i+1
      lc[j]=tc[i+1][j] i range(n) range n for j in range(n):
    t=p[tp[i][j]]
    tp[i+1][j]=t
    tc[i+1][j]=tc[i][j]+c[t]
    if t==j and lt[j]==0:
      lt[j]=i+1
      lc[j]=tc[i+1][j] j range(n) range n t=p[tp[i][j]] t p[tp[i][j]] p tp[i][j] [i] tp i j tp[i+1][j]=t tp[i+1][j] [i+1] tp i+1 i 1 j t tc[i+1][j]=tc[i][j]+c[t] tc[i+1][j] [i+1] tc i+1 i 1 j tc[i][j]+c[t] tc[i][j] [i] tc i j c[t] c t if t==j and lt[j]==0:
      lt[j]=i+1
      lc[j]=tc[i+1][j] t==j and lt[j]==0 t==j t j lt[j]==0 lt[j] lt j 0 lt[j]=i+1 lt[j] lt j i+1 i 1 lc[j]=tc[i+1][j] lc[j] lc j tc[i+1][j] [i+1] tc i+1 i 1 j a=-INF a -INF INF for i in range(n):
  if lc[i]<1 or k<=lt[i]:
    for j in range(min(lt[i],k)):
      a=max(a,tc[j+1][i])
  else:
    for j in range(lt[i]):
      t=k//lt[i]*lc[i]+tc[j+1][i]
      if k%lt[i]<j+1: t-=lc[i]
      a=max(a,t) i range(n) range n if lc[i]<1 or k<=lt[i]:
    for j in range(min(lt[i],k)):
      a=max(a,tc[j+1][i])
  else:
    for j in range(lt[i]):
      t=k//lt[i]*lc[i]+tc[j+1][i]
      if k%lt[i]<j+1: t-=lc[i]
      a=max(a,t) lc[i]<1 or k<=lt[i] lc[i]<1 lc[i] lc i 1 k<=lt[i] k lt[i] lt i for j in range(min(lt[i],k)):
      a=max(a,tc[j+1][i])
   j range(min(lt[i],k)) range min(lt[i],k) min lt[i] lt i k a=max(a,tc[j+1][i]) a max(a,tc[j+1][i]) max a tc[j+1][i] [j+1] tc j+1 j 1 i for j in range(lt[i]):
      t=k//lt[i]*lc[i]+tc[j+1][i]
      if k%lt[i]<j+1: t-=lc[i]
      a=max(a,t) j range(lt[i]) range lt[i] lt i t=k//lt[i]*lc[i]+tc[j+1][i] t k//lt[i]*lc[i]+tc[j+1][i] k//lt[i]*lc[i] k//lt[i] k lt[i] lt i lc[i] lc i tc[j+1][i] [j+1] tc j+1 j 1 i if k%lt[i]<j+1: t-=lc[i]
       k%lt[i]<j+1 k%lt[i] k lt[i] lt i j+1 j 1 a=max(a,t) a max(a,t) max a t print(a) print a tp=[[*range(n)] for _ in range(n+1)] [[*range(n)] for _ in range(n+1)] tp f=lambda:[*map(int,input().split())] lambda:[*map(int,input().split())] f k=f() f() k c=f(),f() f() c lc=[0]*n [0]*n lc p,c=f(),f() f() p t=k//lt[i]*lc[i]+tc[j+1][i] k//lt[i]*lc[i]+tc[j+1][i] t a=-INF -INF a n,k=f() f() n INF=10**9 10**9 INF tc=[[0]*n for _ in range(n+1)] [[0]*n for _ in range(n+1)] tc a=max(a,t) max(a,t) a lt=[0]*n [0]*n lt p=[*map(lambda x:x-1,p)] [*map(lambda x:x-1,p)] p t=p[tp[i][j]] p[tp[i][j]] t a=max(a,tc[j+1][i]) max(a,tc[j+1][i]) a
INF=10**9
f=lambda:[*map(int,input().split())]
n,k=f()
p,c=f(),f()
p=[*map(lambda x:x-1,p)]
tp=[[*range(n)] for _ in range(n+1)]
tc=[[0]*n for _ in range(n+1)]
lt=[0]*n
lc=[0]*n
for i in range(n):
  for j in range(n):
    t=p[tp[i][j]]
    tp[i+1][j]=t
    tc[i+1][j]=tc[i][j]+c[t]
    if t==j and lt[j]==0:
      lt[j]=i+1
      lc[j]=tc[i+1][j]
a=-INF
for i in range(n):
  if lc[i]<1 or k<=lt[i]:
    for j in range(min(lt[i],k)):
      a=max(a,tc[j+1][i])
  else:
    for j in range(lt[i]):
      t=k//lt[i]*lc[i]+tc[j+1][i]
      if k%lt[i]<j+1: t-=lc[i]
      a=max(a,t)
print(a)