n,k = map(int,input().split())
p = [int(i)-1 for i in input().split()]
c = [int(i) for i in input().split()]
INF = float("INF")
ans = -INF

for si in range(n):
    x = si
    S = []
    tot = 0
    while True:
        x = p[x]
        S.append(c[x])
        tot += c[x]
        if x == si: break
    l = len(S)
    t = 0
    for i in range(l):
        if i+1 > k: break
        t += S[i]
        now = t
        if tot > 0:
            e = (k-(i+1))//l
            now += tot*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 int(i)-1 for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i)-1 int(i) int i 1 p = [int(i)-1 for i in input().split()] p [int(i)-1 for i in input().split()] int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i c = [int(i) for i in input().split()] c [int(i) for i in input().split()] INF = float("INF") INF float("INF") float "INF" ans = -INF ans -INF INF for si in range(n):
    x = si
    S = []
    tot = 0
    while True:
        x = p[x]
        S.append(c[x])
        tot += c[x]
        if x == si: break
    l = len(S)
    t = 0
    for i in range(l):
        if i+1 > k: break
        t += S[i]
        now = t
        if tot > 0:
            e = (k-(i+1))//l
            now += tot*e
        ans = max(ans, now) si range(n) range n x = si x si S = [] S [] tot = 0 tot 0 while True:
        x = p[x]
        S.append(c[x])
        tot += c[x]
        if x == si: break
     True x = p[x] x p[x] p x S.append(c[x]) S.append S append c[x] c x tot += c[x] tot c[x] c x if x == si: break
     x == si x si l = len(S) l len(S) len S t = 0 t 0 for i in range(l):
        if i+1 > k: break
        t += S[i]
        now = t
        if tot > 0:
            e = (k-(i+1))//l
            now += tot*e
        ans = max(ans, now) i range(l) range l if i+1 > k: break
         i+1 > k i+1 i 1 k t += S[i] t S[i] S i now = t now t if tot > 0:
            e = (k-(i+1))//l
            now += tot*e
         tot > 0 tot 0 e = (k-(i+1))//l e (k-(i+1))//l k-(i+1) k i+1 i 1 l now += tot*e now tot*e tot e ans = max(ans, now) ans max(ans, now) max ans now print(ans) print ans x = p[x] p[x] x l = len(S) len(S) l n,k = map(int,input().split()) map(int,input().split()) n ans = -INF -INF ans tot += c[x] c[x] tot now += tot*e tot*e now k = map(int,input().split()) map(int,input().split()) k ans = max(ans, now) max(ans, now) ans S = [] [] S INF = float("INF") float("INF") INF tot = 0 0 tot c = [int(i) for i in input().split()] [int(i) for i in input().split()] c p = [int(i)-1 for i in input().split()] [int(i)-1 for i in input().split()] p t = 0 0 t now = t t now e = (k-(i+1))//l (k-(i+1))//l e x = si si x t += S[i] S[i] t
n,k = map(int,input().split())
p = [int(i)-1 for i in input().split()]
c = [int(i) for i in input().split()]
INF = float("INF")
ans = -INF

for si in range(n):
    x = si
    S = []
    tot = 0
    while True:
        x = p[x]
        S.append(c[x])
        tot += c[x]
        if x == si: break
    l = len(S)
    t = 0
    for i in range(l):
        if i+1 > k: break
        t += S[i]
        now = t
        if tot > 0:
            e = (k-(i+1))//l
            now += tot*e
        ans = max(ans, now)
print(ans)