import sys
input = sys.stdin.readline
n,K=map(int,input().split())
p=list(map(lambda x:int(x)-1,input().split()))
c=list(map(int,input().split()))
if K == 1:
    print(max(c));exit()
ans = -10**10
def cycle(k):
    mo=k
    clen=0
    cscore=0
    flg = True
    cmax=[-10**10]
    while (mo != k or flg):
        clen+=1
        cscore+=c[mo]
        mo = p[mo]
        cmax.append(max(cmax[-1],cscore))
        if mo == k:
            flg = False
    if cscore<0:
        return cmax[min(K-1,clen-1)]
    return cscore*((K-1)//clen)+cmax[(K-1)%clen+1]
for i in range(n):
    a=cycle(i)
    if a>ans:
        ans = a
print(ans) import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline 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 == 1:
    print(max(c));exit() K == 1 K 1 print(max(c)) print max(c) max c exit() exit ans = -10**10 ans -10**10 -10 10 def cycle(k):
    mo=k
    clen=0
    cscore=0
    flg = True
    cmax=[-10**10]
    while (mo != k or flg):
        clen+=1
        cscore+=c[mo]
        mo = p[mo]
        cmax.append(max(cmax[-1],cscore))
        if mo == k:
            flg = False
    if cscore<0:
        return cmax[min(K-1,clen-1)]
    return cscore*((K-1)//clen)+cmax[(K-1)%clen+1] cycle mo=k mo k clen=0 clen 0 cscore=0 cscore 0 flg = True flg True cmax=[-10**10] cmax [-10**10] -10**10 -10 10 while (mo != k or flg):
        clen+=1
        cscore+=c[mo]
        mo = p[mo]
        cmax.append(max(cmax[-1],cscore))
        if mo == k:
            flg = False
     mo != k or flg mo != k mo k flg clen+=1 clen 1 cscore+=c[mo] cscore c[mo] c mo mo = p[mo] mo p[mo] p mo cmax.append(max(cmax[-1],cscore)) cmax.append cmax append max(cmax[-1],cscore) max cmax[-1] cmax -1 cscore if mo == k:
            flg = False
     mo == k mo k flg = False flg False if cscore<0:
        return cmax[min(K-1,clen-1)]
     cscore<0 cscore 0 return cmax[min(K-1,clen-1)] cmax[min(K-1,clen-1)] cmax min(K-1,clen-1) min K-1 K 1 clen-1 clen 1 return cscore*((K-1)//clen)+cmax[(K-1)%clen+1] cscore*((K-1)//clen)+cmax[(K-1)%clen+1] cscore*((K-1)//clen) cscore (K-1)//clen K-1 K 1 clen cmax[(K-1)%clen+1] cmax (K-1)%clen+1 (K-1)%clen K-1 K 1 clen 1 k k for i in range(n):
    a=cycle(i)
    if a>ans:
        ans = a i range(n) range n a=cycle(i) a cycle(i) cycle i if a>ans:
        ans = a a>ans a ans ans = a ans a print(ans) print ans K=map(int,input().split()) map(int,input().split()) K p=list(map(lambda x:int(x)-1,input().split())) list(map(lambda x:int(x)-1,input().split())) p ans = -10**10 -10**10 ans input = sys.stdin.readline sys.stdin.readline input c=list(map(int,input().split())) list(map(int,input().split())) c def cycle(k):
    mo=k
    clen=0
    cscore=0
    flg = True
    cmax=[-10**10]
    while (mo != k or flg):
        clen+=1
        cscore+=c[mo]
        mo = p[mo]
        cmax.append(max(cmax[-1],cscore))
        if mo == k:
            flg = False
    if cscore<0:
        return cmax[min(K-1,clen-1)]
    return cscore*((K-1)//clen)+cmax[(K-1)%clen+1] def cycle(k):
    mo=k
    clen=0
    cscore=0
    flg = True
    cmax=[-10**10]
    while (mo != k or flg):
        clen+=1
        cscore+=c[mo]
        mo = p[mo]
        cmax.append(max(cmax[-1],cscore))
        if mo == k:
            flg = False
    if cscore<0:
        return cmax[min(K-1,clen-1)]
    return cscore*((K-1)//clen)+cmax[(K-1)%clen+1] cycle ans = a a ans n,K=map(int,input().split()) map(int,input().split()) n a=cycle(i) cycle(i) a
import sys
input = sys.stdin.readline
n,K=map(int,input().split())
p=list(map(lambda x:int(x)-1,input().split()))
c=list(map(int,input().split()))
if K == 1:
    print(max(c));exit()
ans = -10**10
def cycle(k):
    mo=k
    clen=0
    cscore=0
    flg = True
    cmax=[-10**10]
    while (mo != k or flg):
        clen+=1
        cscore+=c[mo]
        mo = p[mo]
        cmax.append(max(cmax[-1],cscore))
        if mo == k:
            flg = False
    if cscore<0:
        return cmax[min(K-1,clen-1)]
    return cscore*((K-1)//clen)+cmax[(K-1)%clen+1]
for i in range(n):
    a=cycle(i)
    if a>ans:
        ans = a
print(ans)
