N, K = [int(x) for x in input().split()]
P = [0] + [int(x) for x in input().split()]
C = [0] + [int(x) for x in input().split()]

max_score = max(C[1:])
for init in range(1, N + 1):   # 初めの場所をinitとする
    score = [0]  # k回移動後のスコア
    i = init
    for k in range(1, K + 1):
        i = P[i]  # k回移動後に着くところ
        score.append(score[-1] + C[i])
        max_score = max(max_score, score[k])
        if i == init:  # ループ検出
            loop_score = score[-1]
            loop_len = k
            if loop_score > 0:
                max_score = max(max_score, max(score[j] + loop_score * ((K - j) // loop_len) for j in range(1, loop_len + 1)))
            break

print(max_score) 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 N, K = [int(x) for x in input().split()] N [int(x) for x in 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 = [0] + [int(x) for x in input().split()] P [0] + [int(x) for x in input().split()] [0] 0 [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 = [0] + [int(x) for x in input().split()] C [0] + [int(x) for x in input().split()] [0] 0 [int(x) for x in input().split()] max_score = max(C[1:]) max_score max(C[1:]) max C[1:] C 1: 1 for init in range(1, N + 1):   # 初めの場所をinitとする
    score = [0]  # k回移動後のスコア
    i = init
    for k in range(1, K + 1):
        i = P[i]  # k回移動後に着くところ
        score.append(score[-1] + C[i])
        max_score = max(max_score, score[k])
        if i == init:  # ループ検出
            loop_score = score[-1]
            loop_len = k
            if loop_score > 0:
                max_score = max(max_score, max(score[j] + loop_score * ((K - j) // loop_len) for j in range(1, loop_len + 1)))
            break init range(1, N + 1) range 1 N + 1 N 1 score = [0] score [0] 0 i = init i init for k in range(1, K + 1):
        i = P[i]  # k回移動後に着くところ
        score.append(score[-1] + C[i])
        max_score = max(max_score, score[k])
        if i == init:  # ループ検出
            loop_score = score[-1]
            loop_len = k
            if loop_score > 0:
                max_score = max(max_score, max(score[j] + loop_score * ((K - j) // loop_len) for j in range(1, loop_len + 1)))
            break k range(1, K + 1) range 1 K + 1 K 1 i = P[i] i P[i] P i score.append(score[-1] + C[i]) score.append score append score[-1] + C[i] score[-1] score -1 C[i] C i max_score = max(max_score, score[k]) max_score max(max_score, score[k]) max max_score score[k] score k if i == init:  # ループ検出
            loop_score = score[-1]
            loop_len = k
            if loop_score > 0:
                max_score = max(max_score, max(score[j] + loop_score * ((K - j) // loop_len) for j in range(1, loop_len + 1)))
            break i == init i init loop_score = score[-1] loop_score score[-1] score -1 loop_len = k loop_len k if loop_score > 0:
                max_score = max(max_score, max(score[j] + loop_score * ((K - j) // loop_len) for j in range(1, loop_len + 1)))
             loop_score > 0 loop_score 0 max_score = max(max_score, max(score[j] + loop_score * ((K - j) // loop_len) for j in range(1, loop_len + 1))) max_score max(max_score, max(score[j] + loop_score * ((K - j) // loop_len) for j in range(1, loop_len + 1))) max max_score max(score[j] + loop_score * ((K - j) // loop_len) for j in range(1, loop_len + 1)) max score[j] + loop_score * ((K - j) // loop_len) score[j] score j loop_score * ((K - j) // loop_len) loop_score (K - j) // loop_len K - j K j loop_len break print(max_score) print max_score max_score = max(C[1:]) max(C[1:]) max_score i = P[i] P[i] i P = [0] + [int(x) for x in input().split()] [0] + [int(x) for x in input().split()] P loop_score = score[-1] score[-1] loop_score C = [0] + [int(x) for x in input().split()] [0] + [int(x) for x in input().split()] C K = [int(x) for x in input().split()] [int(x) for x in input().split()] K max_score = max(max_score, max(score[j] + loop_score * ((K - j) // loop_len) for j in range(1, loop_len + 1))) max(max_score, max(score[j] + loop_score * ((K - j) // loop_len) for j in range(1, loop_len + 1))) max_score N, K = [int(x) for x in input().split()] [int(x) for x in input().split()] N score = [0] [0] score max_score = max(max_score, score[k]) max(max_score, score[k]) max_score i = init init i loop_len = k k loop_len
N, K = [int(x) for x in input().split()]
P = [0] + [int(x) for x in input().split()]
C = [0] + [int(x) for x in input().split()]

max_score = max(C[1:])
for init in range(1, N + 1):   # 初めの場所をinitとする
    score = [0]  # k回移動後のスコア
    i = init
    for k in range(1, K + 1):
        i = P[i]  # k回移動後に着くところ
        score.append(score[-1] + C[i])
        max_score = max(max_score, score[k])
        if i == init:  # ループ検出
            loop_score = score[-1]
            loop_len = k
            if loop_score > 0:
                max_score = max(max_score, max(score[j] + loop_score * ((K - j) // loop_len) for j in range(1, loop_len + 1)))
            break

print(max_score)