n, k = map(int, input().split())

p = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]

p_flag = [0 for _ in range(len(p))]
loop_max = []
loop_len = []
S = []
S_max = []
ans = []

for i in range(len(p)):
    # if p_flag[i] == 1:
    #     continue
    now = i
    temp_len = 0
    temp_s = 0
    S_max = - (10**9 + 1)
    temp_l = []
    while True:
        now = p[now] - 1
        temp_len += 1
        temp_s += c[now]
        temp_l.append(temp_s)
        p_flag[now] = 1

        # 最初に戻ってきたら
        if now == i:
            break

    loop_len.append(temp_len)
    loop_max.append(temp_l)
    S.append(temp_s)

for i in range(len(S)):
    mod = k % loop_len[i] if k % loop_len[i] != 0 else loop_len[i]
    if S[i] > 0:
        if max(loop_max[i][:mod]) > 0:
            ans.append(((k-1)//loop_len[i])*S[i] + max(loop_max[i][:mod]))
        else:
            ans.append(((k - 1) // loop_len[i]) * S[i])
    else:
        ans.append(max(loop_max[i]))

# print(loop_max)
# print(ans)
print(max(ans)) n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x p = [int(x) for x in input().split()] p [int(x) for x in input().split()] int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x c = [int(x) for x in input().split()] c [int(x) for x in input().split()] 0 for _ in range(len(p)) for _ in range(len(p)) _ range(len(p)) range len(p) len p for _ in range(len(p)) 0 p_flag = [0 for _ in range(len(p))] p_flag [0 for _ in range(len(p))] loop_max = [] loop_max [] loop_len = [] loop_len [] S = [] S [] S_max = [] S_max [] ans = [] ans [] for i in range(len(p)):
    # if p_flag[i] == 1:
    #     continue
    now = i
    temp_len = 0
    temp_s = 0
    S_max = - (10**9 + 1)
    temp_l = []
    while True:
        now = p[now] - 1
        temp_len += 1
        temp_s += c[now]
        temp_l.append(temp_s)
        p_flag[now] = 1

        # 最初に戻ってきたら
        if now == i:
            break

    loop_len.append(temp_len)
    loop_max.append(temp_l)
    S.append(temp_s) i range(len(p)) range len(p) len p now = i now i temp_len = 0 temp_len 0 temp_s = 0 temp_s 0 S_max = - (10**9 + 1) S_max - (10**9 + 1) 10**9 + 1 10**9 10 9 1 temp_l = [] temp_l [] while True:
        now = p[now] - 1
        temp_len += 1
        temp_s += c[now]
        temp_l.append(temp_s)
        p_flag[now] = 1

        # 最初に戻ってきたら
        if now == i:
            break

     True now = p[now] - 1 now p[now] - 1 p[now] p now 1 temp_len += 1 temp_len 1 temp_s += c[now] temp_s c[now] c now temp_l.append(temp_s) temp_l.append temp_l append temp_s p_flag[now] = 1 p_flag[now] p_flag now 1 if now == i:
            break

     now == i now i break loop_len.append(temp_len) loop_len.append loop_len append temp_len loop_max.append(temp_l) loop_max.append loop_max append temp_l S.append(temp_s) S.append S append temp_s for i in range(len(S)):
    mod = k % loop_len[i] if k % loop_len[i] != 0 else loop_len[i]
    if S[i] > 0:
        if max(loop_max[i][:mod]) > 0:
            ans.append(((k-1)//loop_len[i])*S[i] + max(loop_max[i][:mod]))
        else:
            ans.append(((k - 1) // loop_len[i]) * S[i])
    else:
        ans.append(max(loop_max[i]))

# print(loop_max)
# print(ans) i range(len(S)) range len(S) len S mod = k % loop_len[i] if k % loop_len[i] != 0 else loop_len[i] mod k % loop_len[i] if k % loop_len[i] != 0 else loop_len[i] k % loop_len[i] != 0 k % loop_len[i] k loop_len[i] loop_len i 0 k % loop_len[i] k loop_len[i] loop_len i loop_len[i] loop_len i if S[i] > 0:
        if max(loop_max[i][:mod]) > 0:
            ans.append(((k-1)//loop_len[i])*S[i] + max(loop_max[i][:mod]))
        else:
            ans.append(((k - 1) // loop_len[i]) * S[i])
    else:
        ans.append(max(loop_max[i]))

# print(loop_max)
# print(ans) S[i] > 0 S[i] S i 0 if max(loop_max[i][:mod]) > 0:
            ans.append(((k-1)//loop_len[i])*S[i] + max(loop_max[i][:mod]))
        else:
            ans.append(((k - 1) // loop_len[i]) * S[i])
     max(loop_max[i][:mod]) > 0 max(loop_max[i][:mod]) max loop_max[i][:mod] [i] loop_max i :mod mod 0 ans.append(((k-1)//loop_len[i])*S[i] + max(loop_max[i][:mod])) ans.append ans append ((k-1)//loop_len[i])*S[i] + max(loop_max[i][:mod]) ((k-1)//loop_len[i])*S[i] (k-1)//loop_len[i] k-1 k 1 loop_len[i] loop_len i S[i] S i max(loop_max[i][:mod]) max loop_max[i][:mod] [i] loop_max i :mod mod ans.append(((k - 1) // loop_len[i]) * S[i]) ans.append ans append ((k - 1) // loop_len[i]) * S[i] (k - 1) // loop_len[i] k - 1 k 1 loop_len[i] loop_len i S[i] S i ans.append(max(loop_max[i])) ans.append ans append max(loop_max[i]) max loop_max[i] loop_max i print(max(ans)) print max(ans) max ans S_max = [] [] S_max temp_s = 0 0 temp_s now = p[now] - 1 p[now] - 1 now ans = [] [] ans temp_len = 0 0 temp_len n, k = map(int, input().split()) map(int, input().split()) n now = i i now p_flag = [0 for _ in range(len(p))] [0 for _ in range(len(p))] p_flag temp_l = [] [] temp_l temp_s += c[now] c[now] temp_s S_max = - (10**9 + 1) - (10**9 + 1) S_max k = map(int, input().split()) map(int, input().split()) k c = [int(x) for x in input().split()] [int(x) for x in input().split()] c p = [int(x) for x in input().split()] [int(x) for x in input().split()] p loop_max = [] [] loop_max mod = k % loop_len[i] if k % loop_len[i] != 0 else loop_len[i] k % loop_len[i] if k % loop_len[i] != 0 else loop_len[i] mod loop_len = [] [] loop_len temp_len += 1 1 temp_len S = [] [] S
n, k = map(int, input().split())

p = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]

p_flag = [0 for _ in range(len(p))]
loop_max = []
loop_len = []
S = []
S_max = []
ans = []

for i in range(len(p)):
    # if p_flag[i] == 1:
    #     continue
    now = i
    temp_len = 0
    temp_s = 0
    S_max = - (10**9 + 1)
    temp_l = []
    while True:
        now = p[now] - 1
        temp_len += 1
        temp_s += c[now]
        temp_l.append(temp_s)
        p_flag[now] = 1

        # 最初に戻ってきたら
        if now == i:
            break

    loop_len.append(temp_len)
    loop_max.append(temp_l)
    S.append(temp_s)

for i in range(len(S)):
    mod = k % loop_len[i] if k % loop_len[i] != 0 else loop_len[i]
    if S[i] > 0:
        if max(loop_max[i][:mod]) > 0:
            ans.append(((k-1)//loop_len[i])*S[i] + max(loop_max[i][:mod]))
        else:
            ans.append(((k - 1) // loop_len[i]) * S[i])
    else:
        ans.append(max(loop_max[i]))

# print(loop_max)
# print(ans)
print(max(ans))
