import numpy as np
n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
 
cycles, check = [], [False]*n
for pi in p:
    if not check[pi-1]:
        curr, cycle = pi, []
        check[pi-1] = True
        while pi != p[curr-1]:
            check[curr-1] = True
            cycle.append(c[curr-1])
            curr = p[curr-1]
        check[curr-1] = True
        cycles.append(cycle + [c[curr-1]])

scores = []
for cycle in (np.array(cycle) for cycle in cycles):
    for i in range(cycle.size):
        cum = np.roll(cycle, i).cumsum()
        if cum[-1] > 0 and k > cycle.size:
            scores.append(cum[-1]*(k//cycle.size-1) + np.concatenate([cum, cum[:k % cycle.size]+cum[-1]]).max())
        else:
            scores.append(cum[:min(k, cum.size)].max())
print(max(scores)) import numpy as np numpy 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 cycles, check = [], [False]*n cycles [] check [False]*n [False] False n for pi in p:
    if not check[pi-1]:
        curr, cycle = pi, []
        check[pi-1] = True
        while pi != p[curr-1]:
            check[curr-1] = True
            cycle.append(c[curr-1])
            curr = p[curr-1]
        check[curr-1] = True
        cycles.append(cycle + [c[curr-1]]) pi p if not check[pi-1]:
        curr, cycle = pi, []
        check[pi-1] = True
        while pi != p[curr-1]:
            check[curr-1] = True
            cycle.append(c[curr-1])
            curr = p[curr-1]
        check[curr-1] = True
        cycles.append(cycle + [c[curr-1]]) not check[pi-1] check[pi-1] check pi-1 pi 1 curr, cycle = pi, [] curr pi cycle [] check[pi-1] = True check[pi-1] check pi-1 pi 1 True while pi != p[curr-1]:
            check[curr-1] = True
            cycle.append(c[curr-1])
            curr = p[curr-1]
         pi != p[curr-1] pi p[curr-1] p curr-1 curr 1 check[curr-1] = True check[curr-1] check curr-1 curr 1 True cycle.append(c[curr-1]) cycle.append cycle append c[curr-1] c curr-1 curr 1 curr = p[curr-1] curr p[curr-1] p curr-1 curr 1 check[curr-1] = True check[curr-1] check curr-1 curr 1 True cycles.append(cycle + [c[curr-1]]) cycles.append cycles append cycle + [c[curr-1]] cycle [c[curr-1]] c[curr-1] c curr-1 curr 1 scores = [] scores [] for cycle in (np.array(cycle) for cycle in cycles):
    for i in range(cycle.size):
        cum = np.roll(cycle, i).cumsum()
        if cum[-1] > 0 and k > cycle.size:
            scores.append(cum[-1]*(k//cycle.size-1) + np.concatenate([cum, cum[:k % cycle.size]+cum[-1]]).max())
        else:
            scores.append(cum[:min(k, cum.size)].max()) cycle (np.array(cycle) for cycle in cycles) for i in range(cycle.size):
        cum = np.roll(cycle, i).cumsum()
        if cum[-1] > 0 and k > cycle.size:
            scores.append(cum[-1]*(k//cycle.size-1) + np.concatenate([cum, cum[:k % cycle.size]+cum[-1]]).max())
        else:
            scores.append(cum[:min(k, cum.size)].max()) i range(cycle.size) range cycle.size cycle size cum = np.roll(cycle, i).cumsum() cum np.roll(cycle, i).cumsum() np.roll(cycle, i).cumsum np.roll(cycle, i) np.roll np roll cycle i cumsum if cum[-1] > 0 and k > cycle.size:
            scores.append(cum[-1]*(k//cycle.size-1) + np.concatenate([cum, cum[:k % cycle.size]+cum[-1]]).max())
        else:
            scores.append(cum[:min(k, cum.size)].max()) cum[-1] > 0 and k > cycle.size cum[-1] > 0 cum[-1] cum -1 0 k > cycle.size k cycle.size cycle size scores.append(cum[-1]*(k//cycle.size-1) + np.concatenate([cum, cum[:k % cycle.size]+cum[-1]]).max()) scores.append scores append cum[-1]*(k//cycle.size-1) + np.concatenate([cum, cum[:k % cycle.size]+cum[-1]]).max() cum[-1]*(k//cycle.size-1) cum[-1] cum -1 k//cycle.size-1 k//cycle.size k cycle.size cycle size 1 np.concatenate([cum, cum[:k % cycle.size]+cum[-1]]).max() np.concatenate([cum, cum[:k % cycle.size]+cum[-1]]).max np.concatenate([cum, cum[:k % cycle.size]+cum[-1]]) np.concatenate np concatenate [cum, cum[:k % cycle.size]+cum[-1]] cum cum[:k % cycle.size]+cum[-1] cum[:k % cycle.size] cum :k % cycle.size k % cycle.size k cycle.size cycle size cum[-1] cum -1 max scores.append(cum[:min(k, cum.size)].max()) scores.append scores append cum[:min(k, cum.size)].max() [:min(k, cum.size)].max [:min(k, cum.size)] cum :min(k, cum.size) min(k, cum.size) min k cum.size cum size max print(max(scores)) print max(scores) max scores k = map(int, input().split()) map(int, input().split()) k check = [], [False]*n [False]*n check curr = p[curr-1] p[curr-1] curr n, k = map(int, input().split()) map(int, input().split()) n curr, cycle = pi, [] pi curr scores = [] [] scores p = list(map(int, input().split())) list(map(int, input().split())) p cum = np.roll(cycle, i).cumsum() np.roll(cycle, i).cumsum() cum cycles, check = [], [False]*n [] cycles c = list(map(int, input().split())) list(map(int, input().split())) c cycle = pi, [] [] cycle
import numpy as np
n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
 
cycles, check = [], [False]*n
for pi in p:
    if not check[pi-1]:
        curr, cycle = pi, []
        check[pi-1] = True
        while pi != p[curr-1]:
            check[curr-1] = True
            cycle.append(c[curr-1])
            curr = p[curr-1]
        check[curr-1] = True
        cycles.append(cycle + [c[curr-1]])

scores = []
for cycle in (np.array(cycle) for cycle in cycles):
    for i in range(cycle.size):
        cum = np.roll(cycle, i).cumsum()
        if cum[-1] > 0 and k > cycle.size:
            scores.append(cum[-1]*(k//cycle.size-1) + np.concatenate([cum, cum[:k % cycle.size]+cum[-1]]).max())
        else:
            scores.append(cum[:min(k, cum.size)].max())
print(max(scores))