N,K = map(int,input().split())
P = list(map(int,input().split()))
C = list(map(int,input().split()))

ans = -1*float('inf')

for i in range(N):
    k = i
    Cc = []
    Ct = 0
    while 1:
        k = P[k]-1
        Cc.append(C[k])
        if i == k:
            break
    l = len(Cc)
    Ct = sum(Cc)
    t = 0
    for j in range(l):
        t += Cc[j]
        if j >= K:break
        now = t
        if Ct > 0:
            e = (K-j-1)//l
            now +=  Ct*e
        ans = max(ans,now)
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 ans = -1*float('inf') ans -1*float('inf') -1 float('inf') float 'inf' for i in range(N):
    k = i
    Cc = []
    Ct = 0
    while 1:
        k = P[k]-1
        Cc.append(C[k])
        if i == k:
            break
    l = len(Cc)
    Ct = sum(Cc)
    t = 0
    for j in range(l):
        t += Cc[j]
        if j >= K:break
        now = t
        if Ct > 0:
            e = (K-j-1)//l
            now +=  Ct*e
        ans = max(ans,now) i range(N) range N k = i k i Cc = [] Cc [] Ct = 0 Ct 0 while 1:
        k = P[k]-1
        Cc.append(C[k])
        if i == k:
            break
     1 k = P[k]-1 k P[k]-1 P[k] P k 1 Cc.append(C[k]) Cc.append Cc append C[k] C k if i == k:
            break
     i == k i k break l = len(Cc) l len(Cc) len Cc Ct = sum(Cc) Ct sum(Cc) sum Cc t = 0 t 0 for j in range(l):
        t += Cc[j]
        if j >= K:break
        now = t
        if Ct > 0:
            e = (K-j-1)//l
            now +=  Ct*e
        ans = max(ans,now) j range(l) range l t += Cc[j] t Cc[j] Cc j if j >= K:break
         j >= K j K now = t now t if Ct > 0:
            e = (K-j-1)//l
            now +=  Ct*e
         Ct > 0 Ct 0 e = (K-j-1)//l e (K-j-1)//l K-j-1 K-j K j 1 l now +=  Ct*e now Ct*e Ct e ans = max(ans,now) ans max(ans,now) max ans now print(ans) print ans C = list(map(int,input().split())) list(map(int,input().split())) C l = len(Cc) len(Cc) l now +=  Ct*e Ct*e now Cc = [] [] Cc P = list(map(int,input().split())) list(map(int,input().split())) P t = 0 0 t Ct = sum(Cc) sum(Cc) Ct e = (K-j-1)//l (K-j-1)//l e k = i i k ans = -1*float('inf') -1*float('inf') ans k = P[k]-1 P[k]-1 k t += Cc[j] Cc[j] t K = map(int,input().split()) map(int,input().split()) K N,K = map(int,input().split()) map(int,input().split()) N Ct = 0 0 Ct ans = max(ans,now) max(ans,now) ans now = t t now
N,K = map(int,input().split())
P = list(map(int,input().split()))
C = list(map(int,input().split()))

ans = -1*float('inf')

for i in range(N):
    k = i
    Cc = []
    Ct = 0
    while 1:
        k = P[k]-1
        Cc.append(C[k])
        if i == k:
            break
    l = len(Cc)
    Ct = sum(Cc)
    t = 0
    for j in range(l):
        t += Cc[j]
        if j >= K:break
        now = t
        if Ct > 0:
            e = (K-j-1)//l
            now +=  Ct*e
        ans = max(ans,now)
print(ans)