N, K = map(int, input().split())
Ps = list(map(int, input().split()))
Ps = [i - 1 for i in Ps]
Cs = list(map(int, input().split()))
max_score = -float("inf")


for i in range(N):
    scores = []
    idx = i  # starting index
    while True:
        idx = Ps[idx]  # point to the next index
        scores.append(Cs[idx])  # fetch a score to add
        if idx == i:  # when the cycle is closed
            break

    len_cycle = len(scores)
    sum_cycle = sum(scores)

    if sum_cycle <= 0:  # when each cycle reduces the total score
        score = 0
        for j in range(min(K, len_cycle)):
            score += scores[j]
            max_score = max(max_score, score)
    else:
        if K // len_cycle > 0:
            num_multiply = K // len_cycle - 1
            score = sum_cycle * num_multiply
            scores = scores * 2
            for j in range(K % len_cycle + len_cycle):
                score += scores[j]
                max_score = max(max_score, score)
        else:
            num_multiply = 0  # len_cycle > K
            score = sum_cycle * num_multiply
            for j in range(K):
                score += scores[j]
                max_score = max(max_score, score)

print(max_score) N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K Ps = list(map(int, input().split())) Ps list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split i - 1 for i in Ps for i in Ps i Ps for i in Ps i - 1 i 1 Ps = [i - 1 for i in Ps] Ps [i - 1 for i in Ps] Cs = list(map(int, input().split())) Cs list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split max_score = -float("inf") max_score -float("inf") float("inf") float "inf" for i in range(N):
    scores = []
    idx = i  # starting index
    while True:
        idx = Ps[idx]  # point to the next index
        scores.append(Cs[idx])  # fetch a score to add
        if idx == i:  # when the cycle is closed
            break

    len_cycle = len(scores)
    sum_cycle = sum(scores)

    if sum_cycle <= 0:  # when each cycle reduces the total score
        score = 0
        for j in range(min(K, len_cycle)):
            score += scores[j]
            max_score = max(max_score, score)
    else:
        if K // len_cycle > 0:
            num_multiply = K // len_cycle - 1
            score = sum_cycle * num_multiply
            scores = scores * 2
            for j in range(K % len_cycle + len_cycle):
                score += scores[j]
                max_score = max(max_score, score)
        else:
            num_multiply = 0  # len_cycle > K
            score = sum_cycle * num_multiply
            for j in range(K):
                score += scores[j]
                max_score = max(max_score, score) i range(N) range N scores = [] scores [] idx = i idx i while True:
        idx = Ps[idx]  # point to the next index
        scores.append(Cs[idx])  # fetch a score to add
        if idx == i:  # when the cycle is closed
            break

     True idx = Ps[idx] idx Ps[idx] Ps idx scores.append(Cs[idx]) scores.append scores append Cs[idx] Cs idx if idx == i:  # when the cycle is closed
            break

     idx == i idx i break len_cycle = len(scores) len_cycle len(scores) len scores sum_cycle = sum(scores) sum_cycle sum(scores) sum scores if sum_cycle <= 0:  # when each cycle reduces the total score
        score = 0
        for j in range(min(K, len_cycle)):
            score += scores[j]
            max_score = max(max_score, score)
    else:
        if K // len_cycle > 0:
            num_multiply = K // len_cycle - 1
            score = sum_cycle * num_multiply
            scores = scores * 2
            for j in range(K % len_cycle + len_cycle):
                score += scores[j]
                max_score = max(max_score, score)
        else:
            num_multiply = 0  # len_cycle > K
            score = sum_cycle * num_multiply
            for j in range(K):
                score += scores[j]
                max_score = max(max_score, score) sum_cycle <= 0 sum_cycle 0 score = 0 score 0 for j in range(min(K, len_cycle)):
            score += scores[j]
            max_score = max(max_score, score)
     j range(min(K, len_cycle)) range min(K, len_cycle) min K len_cycle score += scores[j] score scores[j] scores j max_score = max(max_score, score) max_score max(max_score, score) max max_score score if K // len_cycle > 0:
            num_multiply = K // len_cycle - 1
            score = sum_cycle * num_multiply
            scores = scores * 2
            for j in range(K % len_cycle + len_cycle):
                score += scores[j]
                max_score = max(max_score, score)
        else:
            num_multiply = 0  # len_cycle > K
            score = sum_cycle * num_multiply
            for j in range(K):
                score += scores[j]
                max_score = max(max_score, score) K // len_cycle > 0 K // len_cycle K len_cycle 0 num_multiply = K // len_cycle - 1 num_multiply K // len_cycle - 1 K // len_cycle K len_cycle 1 score = sum_cycle * num_multiply score sum_cycle * num_multiply sum_cycle num_multiply scores = scores * 2 scores scores * 2 scores 2 for j in range(K % len_cycle + len_cycle):
                score += scores[j]
                max_score = max(max_score, score)
         j range(K % len_cycle + len_cycle) range K % len_cycle + len_cycle K % len_cycle K len_cycle len_cycle score += scores[j] score scores[j] scores j max_score = max(max_score, score) max_score max(max_score, score) max max_score score num_multiply = 0 num_multiply 0 score = sum_cycle * num_multiply score sum_cycle * num_multiply sum_cycle num_multiply for j in range(K):
                score += scores[j]
                max_score = max(max_score, score) j range(K) range K score += scores[j] score scores[j] scores j max_score = max(max_score, score) max_score max(max_score, score) max max_score score print(max_score) print max_score max_score = -float("inf") -float("inf") max_score score = 0 0 score score = sum_cycle * num_multiply sum_cycle * num_multiply score scores = scores * 2 scores * 2 scores max_score = max(max_score, score) max(max_score, score) max_score score = sum_cycle * num_multiply sum_cycle * num_multiply score num_multiply = K // len_cycle - 1 K // len_cycle - 1 num_multiply N, K = map(int, input().split()) map(int, input().split()) N scores = [] [] scores idx = i i idx score += scores[j] scores[j] score Ps = [i - 1 for i in Ps] [i - 1 for i in Ps] Ps len_cycle = len(scores) len(scores) len_cycle sum_cycle = sum(scores) sum(scores) sum_cycle Cs = list(map(int, input().split())) list(map(int, input().split())) Cs idx = Ps[idx] Ps[idx] idx score += scores[j] scores[j] score score += scores[j] scores[j] score max_score = max(max_score, score) max(max_score, score) max_score max_score = max(max_score, score) max(max_score, score) max_score K = map(int, input().split()) map(int, input().split()) K num_multiply = 0 0 num_multiply Ps = list(map(int, input().split())) list(map(int, input().split())) Ps
N, K = map(int, input().split())
Ps = list(map(int, input().split()))
Ps = [i - 1 for i in Ps]
Cs = list(map(int, input().split()))
max_score = -float("inf")


for i in range(N):
    scores = []
    idx = i  # starting index
    while True:
        idx = Ps[idx]  # point to the next index
        scores.append(Cs[idx])  # fetch a score to add
        if idx == i:  # when the cycle is closed
            break

    len_cycle = len(scores)
    sum_cycle = sum(scores)

    if sum_cycle <= 0:  # when each cycle reduces the total score
        score = 0
        for j in range(min(K, len_cycle)):
            score += scores[j]
            max_score = max(max_score, score)
    else:
        if K // len_cycle > 0:
            num_multiply = K // len_cycle - 1
            score = sum_cycle * num_multiply
            scores = scores * 2
            for j in range(K % len_cycle + len_cycle):
                score += scores[j]
                max_score = max(max_score, score)
        else:
            num_multiply = 0  # len_cycle > K
            score = sum_cycle * num_multiply
            for j in range(K):
                score += scores[j]
                max_score = max(max_score, score)

print(max_score)
