#!/usr/bin python3
# -*- coding: utf-8 -*-

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

ret = - 10 ** 18
for s in range(n):
    nxt = p[s]
    S = [c[nxt]]
    while nxt != s:
        nxt = p[nxt]
        S.append(S[-1]+c[nxt])

    if k<=len(S):
        pt = max(S[:k])
    else:
        if S[-1] <= 0:
            pt = max(S)
        else:
            pt = max(S)
            if k%len(S)!=0:
                pt = max(pt, S[-1]+max(S[:k%len(S)]))
            pt += S[-1]*(k//len(S)-1)
    ret = max(ret, pt)
print(ret) 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()] c = list(map(int, input().split())) c list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split ret = - 10 ** 18 ret - 10 ** 18 - 10 18 for s in range(n):
    nxt = p[s]
    S = [c[nxt]]
    while nxt != s:
        nxt = p[nxt]
        S.append(S[-1]+c[nxt])

    if k<=len(S):
        pt = max(S[:k])
    else:
        if S[-1] <= 0:
            pt = max(S)
        else:
            pt = max(S)
            if k%len(S)!=0:
                pt = max(pt, S[-1]+max(S[:k%len(S)]))
            pt += S[-1]*(k//len(S)-1)
    ret = max(ret, pt) s range(n) range n nxt = p[s] nxt p[s] p s S = [c[nxt]] S [c[nxt]] c[nxt] c nxt while nxt != s:
        nxt = p[nxt]
        S.append(S[-1]+c[nxt])

     nxt != s nxt s nxt = p[nxt] nxt p[nxt] p nxt S.append(S[-1]+c[nxt]) S.append S append S[-1]+c[nxt] S[-1] S -1 c[nxt] c nxt if k<=len(S):
        pt = max(S[:k])
    else:
        if S[-1] <= 0:
            pt = max(S)
        else:
            pt = max(S)
            if k%len(S)!=0:
                pt = max(pt, S[-1]+max(S[:k%len(S)]))
            pt += S[-1]*(k//len(S)-1)
     k<=len(S) k len(S) len S pt = max(S[:k]) pt max(S[:k]) max S[:k] S :k k if S[-1] <= 0:
            pt = max(S)
        else:
            pt = max(S)
            if k%len(S)!=0:
                pt = max(pt, S[-1]+max(S[:k%len(S)]))
            pt += S[-1]*(k//len(S)-1)
     S[-1] <= 0 S[-1] S -1 0 pt = max(S) pt max(S) max S pt = max(S) pt max(S) max S if k%len(S)!=0:
                pt = max(pt, S[-1]+max(S[:k%len(S)]))
             k%len(S)!=0 k%len(S) k len(S) len S 0 pt = max(pt, S[-1]+max(S[:k%len(S)])) pt max(pt, S[-1]+max(S[:k%len(S)])) max pt S[-1]+max(S[:k%len(S)]) S[-1] S -1 max(S[:k%len(S)]) max S[:k%len(S)] S :k%len(S) k%len(S) k len(S) len S pt += S[-1]*(k//len(S)-1) pt S[-1]*(k//len(S)-1) S[-1] S -1 k//len(S)-1 k//len(S) k len(S) len S 1 ret = max(ret, pt) ret max(ret, pt) max ret pt print(ret) print ret ret = max(ret, pt) max(ret, pt) ret pt = max(S[:k]) max(S[:k]) pt S = [c[nxt]] [c[nxt]] S pt = max(pt, S[-1]+max(S[:k%len(S)])) max(pt, S[-1]+max(S[:k%len(S)])) pt pt += S[-1]*(k//len(S)-1) S[-1]*(k//len(S)-1) pt nxt = p[s] p[s] nxt p = [int(i)-1 for i in input().split()] [int(i)-1 for i in input().split()] p ret = - 10 ** 18 - 10 ** 18 ret nxt = p[nxt] p[nxt] nxt c = list(map(int, input().split())) list(map(int, input().split())) c pt = max(S) max(S) pt pt = max(S) max(S) pt n, k = map(int, input().split()) map(int, input().split()) n k = map(int, input().split()) map(int, input().split()) k
#!/usr/bin python3
# -*- coding: utf-8 -*-

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

ret = - 10 ** 18
for s in range(n):
    nxt = p[s]
    S = [c[nxt]]
    while nxt != s:
        nxt = p[nxt]
        S.append(S[-1]+c[nxt])

    if k<=len(S):
        pt = max(S[:k])
    else:
        if S[-1] <= 0:
            pt = max(S)
        else:
            pt = max(S)
            if k%len(S)!=0:
                pt = max(pt, S[-1]+max(S[:k%len(S)]))
            pt += S[-1]*(k//len(S)-1)
    ret = max(ret, pt)
print(ret)
