n, k = map(int, input().split())
p = [0] + list(map(int, input().split()))
c = [0] + list(map(int, input().split()))

ans = - 10 ** 18
for i in range(1, n+1):
    base = i
    nxt = p[i]
    cnt = 0
    score = 0
    while True:
        cnt += 1
        if cnt > k:
            break
        score += c[nxt]
        ans = max(ans, score)
        #print("i = ", i, "ans = ", ans, "score = ", score)
        if nxt == base:
            break
        nxt = p[nxt]
    if cnt >= k:
        continue
    extra = k - cnt
    score_tmp = score
    nxt = p[nxt]
    nxt_tmp = nxt
    for j in range(min(extra, cnt)):
        score += c[nxt]
        ans = max(ans, score)
        #print("ans = ", ans, "score = ", score, "nxt = ", nxt, "c[nxt] = ", c[nxt])
        nxt = p[nxt]
    #print("score = ", score, "ans = ", ans)
    score = score_tmp
    nxt = nxt_tmp
    a = extra // cnt
    if extra % cnt == 0:
        a -= 1
    score += score * max(a, 0)
    ans = max(ans, score)
    for j in range(extra - a * cnt):
        score += c[nxt]
        ans = max(ans, score)
        #print("ans = ", ans, "score = ", score, "nxt = ", nxt, "c[nxt] = ", c[nxt])
        nxt = p[nxt]

print(ans) n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k p = [0] + list(map(int, input().split())) p [0] + list(map(int, input().split())) [0] 0 list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split c = [0] + list(map(int, input().split())) c [0] + list(map(int, input().split())) [0] 0 list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split ans = - 10 ** 18 ans - 10 ** 18 - 10 18 for i in range(1, n+1):
    base = i
    nxt = p[i]
    cnt = 0
    score = 0
    while True:
        cnt += 1
        if cnt > k:
            break
        score += c[nxt]
        ans = max(ans, score)
        #print("i = ", i, "ans = ", ans, "score = ", score)
        if nxt == base:
            break
        nxt = p[nxt]
    if cnt >= k:
        continue
    extra = k - cnt
    score_tmp = score
    nxt = p[nxt]
    nxt_tmp = nxt
    for j in range(min(extra, cnt)):
        score += c[nxt]
        ans = max(ans, score)
        #print("ans = ", ans, "score = ", score, "nxt = ", nxt, "c[nxt] = ", c[nxt])
        nxt = p[nxt]
    #print("score = ", score, "ans = ", ans)
    score = score_tmp
    nxt = nxt_tmp
    a = extra // cnt
    if extra % cnt == 0:
        a -= 1
    score += score * max(a, 0)
    ans = max(ans, score)
    for j in range(extra - a * cnt):
        score += c[nxt]
        ans = max(ans, score)
        #print("ans = ", ans, "score = ", score, "nxt = ", nxt, "c[nxt] = ", c[nxt])
        nxt = p[nxt] i range(1, n+1) range 1 n+1 n 1 base = i base i nxt = p[i] nxt p[i] p i cnt = 0 cnt 0 score = 0 score 0 while True:
        cnt += 1
        if cnt > k:
            break
        score += c[nxt]
        ans = max(ans, score)
        #print("i = ", i, "ans = ", ans, "score = ", score)
        if nxt == base:
            break
        nxt = p[nxt]
     True cnt += 1 cnt 1 if cnt > k:
            break
         cnt > k cnt k break score += c[nxt] score c[nxt] c nxt ans = max(ans, score) ans max(ans, score) max ans score if nxt == base:
            break
         nxt == base nxt base break nxt = p[nxt] nxt p[nxt] p nxt if cnt >= k:
        continue
     cnt >= k cnt k continue extra = k - cnt extra k - cnt k cnt score_tmp = score score_tmp score nxt = p[nxt] nxt p[nxt] p nxt nxt_tmp = nxt nxt_tmp nxt for j in range(min(extra, cnt)):
        score += c[nxt]
        ans = max(ans, score)
        #print("ans = ", ans, "score = ", score, "nxt = ", nxt, "c[nxt] = ", c[nxt])
        nxt = p[nxt]
    #print("score = ", score, "ans = ", ans)
     j range(min(extra, cnt)) range min(extra, cnt) min extra cnt score += c[nxt] score c[nxt] c nxt ans = max(ans, score) ans max(ans, score) max ans score nxt = p[nxt] nxt p[nxt] p nxt score = score_tmp score score_tmp nxt = nxt_tmp nxt nxt_tmp a = extra // cnt a extra // cnt extra cnt if extra % cnt == 0:
        a -= 1
     extra % cnt == 0 extra % cnt extra cnt 0 a -= 1 a 1 score += score * max(a, 0) score score * max(a, 0) score max(a, 0) max a 0 ans = max(ans, score) ans max(ans, score) max ans score for j in range(extra - a * cnt):
        score += c[nxt]
        ans = max(ans, score)
        #print("ans = ", ans, "score = ", score, "nxt = ", nxt, "c[nxt] = ", c[nxt])
        nxt = p[nxt] j range(extra - a * cnt) range extra - a * cnt extra a * cnt a cnt score += c[nxt] score c[nxt] c nxt ans = max(ans, score) ans max(ans, score) max ans score nxt = p[nxt] nxt p[nxt] p nxt print(ans) print ans cnt += 1 1 cnt nxt = p[i] p[i] nxt c = [0] + list(map(int, input().split())) [0] + list(map(int, input().split())) c ans = - 10 ** 18 - 10 ** 18 ans score += c[nxt] c[nxt] score a -= 1 1 a ans = max(ans, score) max(ans, score) ans score = score_tmp score_tmp score nxt = p[nxt] p[nxt] nxt base = i i base k = map(int, input().split()) map(int, input().split()) k p = [0] + list(map(int, input().split())) [0] + list(map(int, input().split())) p nxt = p[nxt] p[nxt] nxt score += c[nxt] c[nxt] score score += c[nxt] c[nxt] score nxt_tmp = nxt nxt nxt_tmp score = 0 0 score nxt = nxt_tmp nxt_tmp nxt score += score * max(a, 0) score * max(a, 0) score ans = max(ans, score) max(ans, score) ans cnt = 0 0 cnt nxt = p[nxt] p[nxt] nxt extra = k - cnt k - cnt extra ans = max(ans, score) max(ans, score) ans nxt = p[nxt] p[nxt] nxt n, k = map(int, input().split()) map(int, input().split()) n ans = max(ans, score) max(ans, score) ans a = extra // cnt extra // cnt a score_tmp = score score score_tmp
n, k = map(int, input().split())
p = [0] + list(map(int, input().split()))
c = [0] + list(map(int, input().split()))

ans = - 10 ** 18
for i in range(1, n+1):
    base = i
    nxt = p[i]
    cnt = 0
    score = 0
    while True:
        cnt += 1
        if cnt > k:
            break
        score += c[nxt]
        ans = max(ans, score)
        #print("i = ", i, "ans = ", ans, "score = ", score)
        if nxt == base:
            break
        nxt = p[nxt]
    if cnt >= k:
        continue
    extra = k - cnt
    score_tmp = score
    nxt = p[nxt]
    nxt_tmp = nxt
    for j in range(min(extra, cnt)):
        score += c[nxt]
        ans = max(ans, score)
        #print("ans = ", ans, "score = ", score, "nxt = ", nxt, "c[nxt] = ", c[nxt])
        nxt = p[nxt]
    #print("score = ", score, "ans = ", ans)
    score = score_tmp
    nxt = nxt_tmp
    a = extra // cnt
    if extra % cnt == 0:
        a -= 1
    score += score * max(a, 0)
    ans = max(ans, score)
    for j in range(extra - a * cnt):
        score += c[nxt]
        ans = max(ans, score)
        #print("ans = ", ans, "score = ", score, "nxt = ", nxt, "c[nxt] = ", c[nxt])
        nxt = p[nxt]

print(ans)