N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

P = [None] + P
C = [None] + C

all_max = C[1]
for st in range(1, N + 1):
    p = P[st]
    scores = [C[p]]

    while p != st and len(scores) < K:
        p = P[p]
        scores.append(C[p])

    num_elem = len(scores)
    all_sum = sum(scores)

    q, r = divmod(K, num_elem)

    max_ = scores[0]
    temp = scores[0]

    max_r = scores[0]
    temp_r = scores[0]
    for x in range(1, num_elem):
        if x < r:
            temp_r += scores[x]
            max_r = max(max_r, temp_r)

        temp += scores[x]
        max_ = max(max_, temp)

    temp_max = scores[0]
    if all_sum > 0 and q > 0:
        if r > 0:
            temp_max = max(all_sum * (q - 1) + max_, all_sum * q + max_r)
        else:
            temp_max = all_sum * (q - 1) + max_
    else:
        temp_max = max_

    all_max = max(all_max, temp_max)


print(all_max) 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 P = [None] + P P [None] + P [None] None P C = [None] + C C [None] + C [None] None C all_max = C[1] all_max C[1] C 1 for st in range(1, N + 1):
    p = P[st]
    scores = [C[p]]

    while p != st and len(scores) < K:
        p = P[p]
        scores.append(C[p])

    num_elem = len(scores)
    all_sum = sum(scores)

    q, r = divmod(K, num_elem)

    max_ = scores[0]
    temp = scores[0]

    max_r = scores[0]
    temp_r = scores[0]
    for x in range(1, num_elem):
        if x < r:
            temp_r += scores[x]
            max_r = max(max_r, temp_r)

        temp += scores[x]
        max_ = max(max_, temp)

    temp_max = scores[0]
    if all_sum > 0 and q > 0:
        if r > 0:
            temp_max = max(all_sum * (q - 1) + max_, all_sum * q + max_r)
        else:
            temp_max = all_sum * (q - 1) + max_
    else:
        temp_max = max_

    all_max = max(all_max, temp_max) st range(1, N + 1) range 1 N + 1 N 1 p = P[st] p P[st] P st scores = [C[p]] scores [C[p]] C[p] C p while p != st and len(scores) < K:
        p = P[p]
        scores.append(C[p])

     p != st and len(scores) < K p != st p st len(scores) < K len(scores) len scores K p = P[p] p P[p] P p scores.append(C[p]) scores.append scores append C[p] C p num_elem = len(scores) num_elem len(scores) len scores all_sum = sum(scores) all_sum sum(scores) sum scores q, r = divmod(K, num_elem) q divmod(K, num_elem) divmod K num_elem r max_ = scores[0] max_ scores[0] scores 0 temp = scores[0] temp scores[0] scores 0 max_r = scores[0] max_r scores[0] scores 0 temp_r = scores[0] temp_r scores[0] scores 0 for x in range(1, num_elem):
        if x < r:
            temp_r += scores[x]
            max_r = max(max_r, temp_r)

        temp += scores[x]
        max_ = max(max_, temp)

     x range(1, num_elem) range 1 num_elem if x < r:
            temp_r += scores[x]
            max_r = max(max_r, temp_r)

         x < r x r temp_r += scores[x] temp_r scores[x] scores x max_r = max(max_r, temp_r) max_r max(max_r, temp_r) max max_r temp_r temp += scores[x] temp scores[x] scores x max_ = max(max_, temp) max_ max(max_, temp) max max_ temp temp_max = scores[0] temp_max scores[0] scores 0 if all_sum > 0 and q > 0:
        if r > 0:
            temp_max = max(all_sum * (q - 1) + max_, all_sum * q + max_r)
        else:
            temp_max = all_sum * (q - 1) + max_
    else:
        temp_max = max_

     all_sum > 0 and q > 0 all_sum > 0 all_sum 0 q > 0 q 0 if r > 0:
            temp_max = max(all_sum * (q - 1) + max_, all_sum * q + max_r)
        else:
            temp_max = all_sum * (q - 1) + max_
     r > 0 r 0 temp_max = max(all_sum * (q - 1) + max_, all_sum * q + max_r) temp_max max(all_sum * (q - 1) + max_, all_sum * q + max_r) max all_sum * (q - 1) + max_ all_sum * (q - 1) all_sum q - 1 q 1 max_ all_sum * q + max_r all_sum * q all_sum q max_r temp_max = all_sum * (q - 1) + max_ temp_max all_sum * (q - 1) + max_ all_sum * (q - 1) all_sum q - 1 q 1 max_ temp_max = max_ temp_max max_ all_max = max(all_max, temp_max) all_max max(all_max, temp_max) max all_max temp_max print(all_max) print all_max P = [None] + P [None] + P P temp_max = max_ max_ temp_max max_r = scores[0] scores[0] max_r temp_r += scores[x] scores[x] temp_r N, K = map(int, input().split()) map(int, input().split()) N temp_max = max(all_sum * (q - 1) + max_, all_sum * q + max_r) max(all_sum * (q - 1) + max_, all_sum * q + max_r) temp_max C = [None] + C [None] + C C max_ = scores[0] scores[0] max_ C = list(map(int, input().split())) list(map(int, input().split())) C num_elem = len(scores) len(scores) num_elem temp_max = scores[0] scores[0] temp_max temp_max = all_sum * (q - 1) + max_ all_sum * (q - 1) + max_ temp_max scores = [C[p]] [C[p]] scores p = P[p] P[p] p r = divmod(K, num_elem) divmod(K, num_elem) r temp_r = scores[0] scores[0] temp_r q, r = divmod(K, num_elem) divmod(K, num_elem) q p = P[st] P[st] p temp += scores[x] scores[x] temp K = map(int, input().split()) map(int, input().split()) K temp = scores[0] scores[0] temp P = list(map(int, input().split())) list(map(int, input().split())) P all_max = max(all_max, temp_max) max(all_max, temp_max) all_max all_sum = sum(scores) sum(scores) all_sum max_ = max(max_, temp) max(max_, temp) max_ max_r = max(max_r, temp_r) max(max_r, temp_r) max_r all_max = C[1] C[1] all_max
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

P = [None] + P
C = [None] + C

all_max = C[1]
for st in range(1, N + 1):
    p = P[st]
    scores = [C[p]]

    while p != st and len(scores) < K:
        p = P[p]
        scores.append(C[p])

    num_elem = len(scores)
    all_sum = sum(scores)

    q, r = divmod(K, num_elem)

    max_ = scores[0]
    temp = scores[0]

    max_r = scores[0]
    temp_r = scores[0]
    for x in range(1, num_elem):
        if x < r:
            temp_r += scores[x]
            max_r = max(max_r, temp_r)

        temp += scores[x]
        max_ = max(max_, temp)

    temp_max = scores[0]
    if all_sum > 0 and q > 0:
        if r > 0:
            temp_max = max(all_sum * (q - 1) + max_, all_sum * q + max_r)
        else:
            temp_max = all_sum * (q - 1) + max_
    else:
        temp_max = max_

    all_max = max(all_max, temp_max)


print(all_max)
