#!/usr/bin/env python3
import sys

input = sys.stdin.readline


def S():
    return input().rstrip()


def I():
    return int(input())


def MI():
    return map(int, input().split())


n, k = MI()
p = list(MI())
c = list(MI())

p.insert(0, 0)
c.insert(0, 0)

ans = -float("INF")
for i in range(1, n + 1):
    next = p[i]
    score = 0
    MAX = -float("INF")

    for j in range(1, k + 1):
        score += c[next]
        MAX = max(MAX, score)
        if next == i:
            loop_score = score
            loop_size = j
            if loop_score > 0:
                q, r = divmod(k, loop_size)
                # 周れるだけ周る-1+余りの回数で最大となるマス動く
                score1 = loop_score * (q - 1) + MAX
                # 周れるだけ周る+余りの回数で最大となるマス動くとき
                score2 = loop_score * q
                MAX = score2
                next = p[i]
                for _ in range(1, r + 1):
                    score2 += c[next]
                    MAX = max(MAX, score2)
                    next = p[next]
                score = max(score1, MAX)
                break
            else:
                # 一周未満
                score = MAX
                break
        else:
            next = p[next]
    else:
        score = MAX

    ans = max(ans, score)


print(int(ans)) import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline def S():
    return input().rstrip() S return input().rstrip() input().rstrip() ().rstrip () input rstrip def I():
    return int(input()) I return int(input()) int(input()) int input() input def MI():
    return map(int, input().split()) MI return map(int, input().split()) map(int, input().split()) map int input().split() ().split () input split n, k = MI() n MI() MI k p = list(MI()) p list(MI()) list MI() MI c = list(MI()) c list(MI()) list MI() MI p.insert(0, 0) p.insert p insert 0 0 c.insert(0, 0) c.insert c insert 0 0 ans = -float("INF") ans -float("INF") float("INF") float "INF" for i in range(1, n + 1):
    next = p[i]
    score = 0
    MAX = -float("INF")

    for j in range(1, k + 1):
        score += c[next]
        MAX = max(MAX, score)
        if next == i:
            loop_score = score
            loop_size = j
            if loop_score > 0:
                q, r = divmod(k, loop_size)
                # 周れるだけ周る-1+余りの回数で最大となるマス動く
                score1 = loop_score * (q - 1) + MAX
                # 周れるだけ周る+余りの回数で最大となるマス動くとき
                score2 = loop_score * q
                MAX = score2
                next = p[i]
                for _ in range(1, r + 1):
                    score2 += c[next]
                    MAX = max(MAX, score2)
                    next = p[next]
                score = max(score1, MAX)
                break
            else:
                # 一周未満
                score = MAX
                break
        else:
            next = p[next]
    else:
        score = MAX

    ans = max(ans, score) i range(1, n + 1) range 1 n + 1 n 1 next = p[i] next p[i] p i score = 0 score 0 MAX = -float("INF") MAX -float("INF") float("INF") float "INF" for j in range(1, k + 1):
        score += c[next]
        MAX = max(MAX, score)
        if next == i:
            loop_score = score
            loop_size = j
            if loop_score > 0:
                q, r = divmod(k, loop_size)
                # 周れるだけ周る-1+余りの回数で最大となるマス動く
                score1 = loop_score * (q - 1) + MAX
                # 周れるだけ周る+余りの回数で最大となるマス動くとき
                score2 = loop_score * q
                MAX = score2
                next = p[i]
                for _ in range(1, r + 1):
                    score2 += c[next]
                    MAX = max(MAX, score2)
                    next = p[next]
                score = max(score1, MAX)
                break
            else:
                # 一周未満
                score = MAX
                break
        else:
            next = p[next]
    else:
        score = MAX

     j range(1, k + 1) range 1 k + 1 k 1 score += c[next] score c[next] c next MAX = max(MAX, score) MAX max(MAX, score) max MAX score if next == i:
            loop_score = score
            loop_size = j
            if loop_score > 0:
                q, r = divmod(k, loop_size)
                # 周れるだけ周る-1+余りの回数で最大となるマス動く
                score1 = loop_score * (q - 1) + MAX
                # 周れるだけ周る+余りの回数で最大となるマス動くとき
                score2 = loop_score * q
                MAX = score2
                next = p[i]
                for _ in range(1, r + 1):
                    score2 += c[next]
                    MAX = max(MAX, score2)
                    next = p[next]
                score = max(score1, MAX)
                break
            else:
                # 一周未満
                score = MAX
                break
        else:
            next = p[next]
     next == i next i loop_score = score loop_score score loop_size = j loop_size j if loop_score > 0:
                q, r = divmod(k, loop_size)
                # 周れるだけ周る-1+余りの回数で最大となるマス動く
                score1 = loop_score * (q - 1) + MAX
                # 周れるだけ周る+余りの回数で最大となるマス動くとき
                score2 = loop_score * q
                MAX = score2
                next = p[i]
                for _ in range(1, r + 1):
                    score2 += c[next]
                    MAX = max(MAX, score2)
                    next = p[next]
                score = max(score1, MAX)
                break
            else:
                # 一周未満
                score = MAX
                break
         loop_score > 0 loop_score 0 q, r = divmod(k, loop_size) q divmod(k, loop_size) divmod k loop_size r score1 = loop_score * (q - 1) + MAX score1 loop_score * (q - 1) + MAX loop_score * (q - 1) loop_score q - 1 q 1 MAX score2 = loop_score * q score2 loop_score * q loop_score q MAX = score2 MAX score2 next = p[i] next p[i] p i for _ in range(1, r + 1):
                    score2 += c[next]
                    MAX = max(MAX, score2)
                    next = p[next]
                 _ range(1, r + 1) range 1 r + 1 r 1 score2 += c[next] score2 c[next] c next MAX = max(MAX, score2) MAX max(MAX, score2) max MAX score2 next = p[next] next p[next] p next score = max(score1, MAX) score max(score1, MAX) max score1 MAX break score = MAX score MAX break next = p[next] next p[next] p next score = MAX score MAX ans = max(ans, score) ans max(ans, score) max ans score print(int(ans)) print int(ans) int ans score = 0 0 score def S():
    return input().rstrip() def S():
    return input().rstrip() S k = MI() MI() k def MI():
    return map(int, input().split()) def MI():
    return map(int, input().split()) MI ans = max(ans, score) max(ans, score) ans MAX = -float("INF") -float("INF") MAX def I():
    return int(input()) def I():
    return int(input()) I score = MAX MAX score score = max(score1, MAX) max(score1, MAX) score q, r = divmod(k, loop_size) divmod(k, loop_size) q score += c[next] c[next] score MAX = max(MAX, score2) max(MAX, score2) MAX next = p[i] p[i] next n, k = MI() MI() n ans = -float("INF") -float("INF") ans r = divmod(k, loop_size) divmod(k, loop_size) r input = sys.stdin.readline sys.stdin.readline input next = p[next] p[next] next score = MAX MAX score next = p[i] p[i] next loop_score = score score loop_score loop_size = j j loop_size score2 = loop_score * q loop_score * q score2 score1 = loop_score * (q - 1) + MAX loop_score * (q - 1) + MAX score1 c = list(MI()) list(MI()) c score2 += c[next] c[next] score2 next = p[next] p[next] next MAX = score2 score2 MAX p = list(MI()) list(MI()) p MAX = max(MAX, score) max(MAX, score) MAX
#!/usr/bin/env python3
import sys

input = sys.stdin.readline


def S():
    return input().rstrip()


def I():
    return int(input())


def MI():
    return map(int, input().split())


n, k = MI()
p = list(MI())
c = list(MI())

p.insert(0, 0)
c.insert(0, 0)

ans = -float("INF")
for i in range(1, n + 1):
    next = p[i]
    score = 0
    MAX = -float("INF")

    for j in range(1, k + 1):
        score += c[next]
        MAX = max(MAX, score)
        if next == i:
            loop_score = score
            loop_size = j
            if loop_score > 0:
                q, r = divmod(k, loop_size)
                # 周れるだけ周る-1+余りの回数で最大となるマス動く
                score1 = loop_score * (q - 1) + MAX
                # 周れるだけ周る+余りの回数で最大となるマス動くとき
                score2 = loop_score * q
                MAX = score2
                next = p[i]
                for _ in range(1, r + 1):
                    score2 += c[next]
                    MAX = max(MAX, score2)
                    next = p[next]
                score = max(score1, MAX)
                break
            else:
                # 一周未満
                score = MAX
                break
        else:
            next = p[next]
    else:
        score = MAX

    ans = max(ans, score)


print(int(ans))
