n,k=list(map(int,input().split()))

p=list(map(int,input().split()))

c=list(map(int,input().split()))

root=[]

mi=set(range(n))

while mi:

  v=mi.pop()

  r=[c[v]]

  while p[v]-1 in mi:

    r.append(c[p[v]-1])

    mi.discard(p[v]-1)

    v=p[v]-1

  root.append(r)

inf=float('inf')

ans=-inf

for r in root:

  ansr=-inf

  k1=k%len(r)

  k0=k//len(r)

  cr=[0]

  tmp=0

  for ri in r:

    tmp+=ri

    cr.append(tmp)

  sumr=cr[-1]

  for ri in r:

    tmp+=ri

    cr.append(tmp)

  for i in range(len(r)):

    for j in range(i,i+min(k,len(r))):

      ansr=max(ansr,cr[j+1]-cr[i])

  if sumr>0:

    ans=max(ans,ansr+sumr*(k0-1))

  else:

    ans=max(ans,ansr)

  if sumr>0:

    ansr=0

    for i in range(len(r)):

      for j in range(i,i+k1):

        ansr=max(ansr,cr[j+1]-cr[i])    

    ans=max(ans,ansr+sumr*k0)

print(ans)

n,k=list(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 list(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 = list(map(int, input().split()))
p = list(map(int, input().split()))
c = list(map(int, input().split()))
root = []
mi = set(range(n))
while mi:
    v = mi.pop()
    r = [c[v]]
    while p[v] - 1 in mi:
        r.append(c[p[v] - 1])
        mi.discard(p[v] - 1)
        v = p[v] - 1
    root.append(r)
inf = float("inf")
ans = -inf
for r in root:
    ansr = -inf
    k1 = k % len(r)
    k0 = k // len(r)
    cr = [0]
    tmp = 0
    for ri in r:
        tmp += ri
        cr.append(tmp)
    sumr = cr[-1]
    for ri in r:
        tmp += ri
        cr.append(tmp)
    for i in range(len(r)):
        for j in range(i, i + min(k, len(r))):
            ansr = max(ansr, cr[j + 1] - cr[i])
    if sumr > 0:
        ans = max(ans, ansr + sumr * (k0 - 1))
    else:
        ans = max(ans, ansr)
    if sumr > 0:
        ansr = 0
        for i in range(len(r)):
            for j in range(i, i + k1):
                ansr = max(ansr, cr[j + 1] - cr[i])
        ans = max(ans, ansr + sumr * k0)
print(ans)

n, k = list(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 list(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)

-root = [] +p = [x - 1 for x in p] +g = {} +# g[id]=[ary] -    v = mi.pop() -    r = [c[v]] -    while p[v] - 1 in mi: -        r.append(c[p[v] - 1]) -        mi.discard(p[v] - 1) -        v = p[v] - 1 -    root.append(r) -inf = float("inf") -ans = -inf -for r in root: -    ansr = -inf -    k1 = k % len(r) -    k0 = k // len(r) -    cr = [0] +    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) -    for ri in r: -        tmp += ri -        cr.append(tmp) -    sumr = cr[-1] -    for ri in r: -        tmp += ri -        cr.append(tmp) -    for i in range(len(r)): -        for j in range(i, i + min(k, len(r))): -            ansr = max(ansr, cr[j + 1] - cr[i]) -    if sumr > 0: -        ans = max(ans, ansr + sumr * (k0 - 1)) -    else: -        ans = max(ans, ansr) -    if sumr > 0: -        ansr = 0 -        for i in range(len(r)): -            for j in range(i, i + k1): -                ansr = max(ansr, cr[j + 1] - cr[i]) -        ans = max(ans, ansr + sumr * k0) +    ary *= 2 +    cary = [tmp] +    for x in ary: +        tmp += x +        cary.append(tmp) +    g[x0] = [cnt, cary] +ans = -float("inf") +for cnt, cary in list(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)