#!/usr/bin/env python3

# input
n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
for i in range(n):
    p[i] -= 1

# calc
ans = -int(1e18)
for si in range(n):
    x = si
    s = []
    opscore = 0 
    while True:
        x = p[x]
        opscore += c[x]
        s.append(c[x])
        if x == si: 
            break

    oplen = len(s)
    t = 0 
    for i in range(oplen):
        t += s[i]
        if i+1 > k:
            break
        now = t 
        if opscore > 0:
            e = (k-(i+1))//oplen
            now += opscore * 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 for i in range(n):
    p[i] -= 1

# calc i range(n) range n p[i] -= 1 p[i] p i 1 ans = -int(1e18) ans -int(1e18) int(1e18) int 1e18 for si in range(n):
    x = si
    s = []
    opscore = 0 
    while True:
        x = p[x]
        opscore += c[x]
        s.append(c[x])
        if x == si: 
            break

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

     True x = p[x] x p[x] p x opscore += c[x] opscore c[x] c x s.append(c[x]) s.append s append c[x] c x if x == si: 
            break

     x == si x si break oplen = len(s) oplen len(s) len s t = 0 t 0 for i in range(oplen):
        t += s[i]
        if i+1 > k:
            break
        now = t 
        if opscore > 0:
            e = (k-(i+1))//oplen
            now += opscore * e 
        ans = max(ans, now) i range(oplen) range oplen t += s[i] t s[i] s i if i+1 > k:
            break
         i+1 > k i+1 i 1 k break now = t now t if opscore > 0:
            e = (k-(i+1))//oplen
            now += opscore * e 
         opscore > 0 opscore 0 e = (k-(i+1))//oplen e (k-(i+1))//oplen k-(i+1) k i+1 i 1 oplen now += opscore * e now opscore * e opscore e ans = max(ans, now) ans max(ans, now) max ans now print(ans) print ans now = t t now x = si si x opscore += c[x] c[x] opscore t = 0 0 t opscore = 0 0 opscore ans = max(ans, now) max(ans, now) ans ans = -int(1e18) -int(1e18) ans c = list(map(int, input().split())) list(map(int, input().split())) c x = p[x] p[x] x p = list(map(int, input().split())) list(map(int, input().split())) p s = [] [] s k = map(int, input().split()) map(int, input().split()) k e = (k-(i+1))//oplen (k-(i+1))//oplen e oplen = len(s) len(s) oplen t += s[i] s[i] t n, k = map(int, input().split()) map(int, input().split()) n now += opscore * e opscore * e now
#!/usr/bin/env python3

# input
n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
for i in range(n):
    p[i] -= 1

# calc
ans = -int(1e18)
for si in range(n):
    x = si
    s = []
    opscore = 0 
    while True:
        x = p[x]
        opscore += c[x]
        s.append(c[x])
        if x == si: 
            break

    oplen = len(s)
    t = 0 
    for i in range(oplen):
        t += s[i]
        if i+1 > k:
            break
        now = t 
        if opscore > 0:
            e = (k-(i+1))//oplen
            now += opscore * e 
        ans = max(ans, now)

print(ans)
