n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
point_start_with = [0 for i in range(n)]
roop_count = [1 for i in range(n)]
for i in range(n):
    now = (i + 1)
    now_score = 0
    while p[now - 1] != (i + 1):
        now = p[now - 1]
        now_score += c[now - 1]
        roop_count[i] += 1
    point_start_with[i] = now_score
# 多分0でいい
ans = -(10**9 * 5000)
for i in range(n):
    # iからスタートする場合
    score = 0
    remain_move = 0
    cand_score = 0
    if point_start_with[i] + c[i] > 0 and k > roop_count[i]:
        # 一周以上する可能性がある場合
        # n週して、残りの動ける回数
        # print("Yes")
        round_count = (k // roop_count[i]) - 1
        remain_move = k - round_count * roop_count[i]
        cand_score = round_count * ( point_start_with[i] + c[i] )
    else:
        remain_move = min(k,roop_count[i])
        score = -(10**9 * 5000)
        cand_score = 0
    now = i
    for j in range(remain_move):
        now = p[now] - 1
        cand_score += c[now]
        score = max(score, cand_score)
    # print("start : {}  score : {}".format(i,score))
    ans = max(score, ans)
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 0 for i in range(n) for i in range(n) i range(n) range n for i in range(n) 0 point_start_with = [0 for i in range(n)] point_start_with [0 for i in range(n)] 1 for i in range(n) for i in range(n) i range(n) range n for i in range(n) 1 roop_count = [1 for i in range(n)] roop_count [1 for i in range(n)] for i in range(n):
    now = (i + 1)
    now_score = 0
    while p[now - 1] != (i + 1):
        now = p[now - 1]
        now_score += c[now - 1]
        roop_count[i] += 1
    point_start_with[i] = now_score
# 多分0でいい i range(n) range n now = (i + 1) now i + 1 i 1 now_score = 0 now_score 0 while p[now - 1] != (i + 1):
        now = p[now - 1]
        now_score += c[now - 1]
        roop_count[i] += 1
     p[now - 1] != (i + 1) p[now - 1] p now - 1 now 1 i + 1 i 1 now = p[now - 1] now p[now - 1] p now - 1 now 1 now_score += c[now - 1] now_score c[now - 1] c now - 1 now 1 roop_count[i] += 1 roop_count[i] roop_count i 1 point_start_with[i] = now_score point_start_with[i] point_start_with i now_score ans = -(10**9 * 5000) ans -(10**9 * 5000) 10**9 * 5000 10**9 10 9 5000 for i in range(n):
    # iからスタートする場合
    score = 0
    remain_move = 0
    cand_score = 0
    if point_start_with[i] + c[i] > 0 and k > roop_count[i]:
        # 一周以上する可能性がある場合
        # n週して、残りの動ける回数
        # print("Yes")
        round_count = (k // roop_count[i]) - 1
        remain_move = k - round_count * roop_count[i]
        cand_score = round_count * ( point_start_with[i] + c[i] )
    else:
        remain_move = min(k,roop_count[i])
        score = -(10**9 * 5000)
        cand_score = 0
    now = i
    for j in range(remain_move):
        now = p[now] - 1
        cand_score += c[now]
        score = max(score, cand_score)
    # print("start : {}  score : {}".format(i,score))
    ans = max(score, ans) i range(n) range n score = 0 score 0 remain_move = 0 remain_move 0 cand_score = 0 cand_score 0 if point_start_with[i] + c[i] > 0 and k > roop_count[i]:
        # 一周以上する可能性がある場合
        # n週して、残りの動ける回数
        # print("Yes")
        round_count = (k // roop_count[i]) - 1
        remain_move = k - round_count * roop_count[i]
        cand_score = round_count * ( point_start_with[i] + c[i] )
    else:
        remain_move = min(k,roop_count[i])
        score = -(10**9 * 5000)
        cand_score = 0
     point_start_with[i] + c[i] > 0 and k > roop_count[i] point_start_with[i] + c[i] > 0 point_start_with[i] + c[i] point_start_with[i] point_start_with i c[i] c i 0 k > roop_count[i] k roop_count[i] roop_count i round_count = (k // roop_count[i]) - 1 round_count (k // roop_count[i]) - 1 k // roop_count[i] k roop_count[i] roop_count i 1 remain_move = k - round_count * roop_count[i] remain_move k - round_count * roop_count[i] k round_count * roop_count[i] round_count roop_count[i] roop_count i cand_score = round_count * ( point_start_with[i] + c[i] ) cand_score round_count * ( point_start_with[i] + c[i] ) round_count point_start_with[i] + c[i] point_start_with[i] point_start_with i c[i] c i remain_move = min(k,roop_count[i]) remain_move min(k,roop_count[i]) min k roop_count[i] roop_count i score = -(10**9 * 5000) score -(10**9 * 5000) 10**9 * 5000 10**9 10 9 5000 cand_score = 0 cand_score 0 now = i now i for j in range(remain_move):
        now = p[now] - 1
        cand_score += c[now]
        score = max(score, cand_score)
    # print("start : {}  score : {}".format(i,score))
     j range(remain_move) range remain_move now = p[now] - 1 now p[now] - 1 p[now] p now 1 cand_score += c[now] cand_score c[now] c now score = max(score, cand_score) score max(score, cand_score) max score cand_score ans = max(score, ans) ans max(score, ans) max score ans print(ans) print ans point_start_with = [0 for i in range(n)] [0 for i in range(n)] point_start_with round_count = (k // roop_count[i]) - 1 (k // roop_count[i]) - 1 round_count n, k = map(int, input().split()) map(int, input().split()) n remain_move = k - round_count * roop_count[i] k - round_count * roop_count[i] remain_move now = (i + 1) i + 1 now score = max(score, cand_score) max(score, cand_score) score ans = max(score, ans) max(score, ans) ans cand_score = round_count * ( point_start_with[i] + c[i] ) round_count * ( point_start_with[i] + c[i] ) cand_score score = -(10**9 * 5000) -(10**9 * 5000) score k = map(int, input().split()) map(int, input().split()) k c = list(map(int, input().split())) list(map(int, input().split())) c roop_count = [1 for i in range(n)] [1 for i in range(n)] roop_count now = p[now] - 1 p[now] - 1 now cand_score += c[now] c[now] cand_score p = list(map(int, input().split())) list(map(int, input().split())) p now_score += c[now - 1] c[now - 1] now_score remain_move = min(k,roop_count[i]) min(k,roop_count[i]) remain_move now = i i now now = p[now - 1] p[now - 1] now cand_score = 0 0 cand_score score = 0 0 score now_score = 0 0 now_score ans = -(10**9 * 5000) -(10**9 * 5000) ans remain_move = 0 0 remain_move cand_score = 0 0 cand_score
n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
point_start_with = [0 for i in range(n)]
roop_count = [1 for i in range(n)]
for i in range(n):
    now = (i + 1)
    now_score = 0
    while p[now - 1] != (i + 1):
        now = p[now - 1]
        now_score += c[now - 1]
        roop_count[i] += 1
    point_start_with[i] = now_score
# 多分0でいい
ans = -(10**9 * 5000)
for i in range(n):
    # iからスタートする場合
    score = 0
    remain_move = 0
    cand_score = 0
    if point_start_with[i] + c[i] > 0 and k > roop_count[i]:
        # 一周以上する可能性がある場合
        # n週して、残りの動ける回数
        # print("Yes")
        round_count = (k // roop_count[i]) - 1
        remain_move = k - round_count * roop_count[i]
        cand_score = round_count * ( point_start_with[i] + c[i] )
    else:
        remain_move = min(k,roop_count[i])
        score = -(10**9 * 5000)
        cand_score = 0
    now = i
    for j in range(remain_move):
        now = p[now] - 1
        cand_score += c[now]
        score = max(score, cand_score)
    # print("start : {}  score : {}".format(i,score))
    ans = max(score, ans)
print(ans)
