# D - Moving Piece

n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
assert len(p) == len(c) == n

visited = [False] * n
scc = []
for i in range(n):
    if not visited[i]:
        scc.append([])
        j = i
        while not visited[j]:
            visited[j] = True
            scc[-1].append(j)
            j = p[j] - 1

n_scc = len(scc)
subsum = [[0] for i in range(n_scc)]
for i in range(n_scc):
    for j in scc[i]:
        subsum[i].append(subsum[i][-1] + c[j])
    for j in scc[i]:
        subsum[i].append(subsum[i][-1] + c[j])

def lister(k):
    for i in range(n_scc):
        l = len(scc[i])
        loop_score = max(0, subsum[i][l])
        for kk in range(1, min(k, l) + 1):
            base = loop_score * ((k - kk) // l)
            for j in range(kk, l + kk + 1):
                yield base + subsum[i][j] - subsum[i][j - kk]

print(max(lister(k))) 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 assert len(p) == len(c) == n len(p) == len(c) == n len(p) == len(c) len(p) len p len(c) len c n visited = [False] * n visited [False] * n [False] False n scc = [] scc [] for i in range(n):
    if not visited[i]:
        scc.append([])
        j = i
        while not visited[j]:
            visited[j] = True
            scc[-1].append(j)
            j = p[j] - 1 i range(n) range n if not visited[i]:
        scc.append([])
        j = i
        while not visited[j]:
            visited[j] = True
            scc[-1].append(j)
            j = p[j] - 1 not visited[i] visited[i] visited i scc.append([]) scc.append scc append [] j = i j i while not visited[j]:
            visited[j] = True
            scc[-1].append(j)
            j = p[j] - 1 not visited[j] visited[j] visited j visited[j] = True visited[j] visited j True scc[-1].append(j) [-1].append [-1] scc -1 append j j = p[j] - 1 j p[j] - 1 p[j] p j 1 n_scc = len(scc) n_scc len(scc) len scc [0] for i in range(n_scc) for i in range(n_scc) i range(n_scc) range n_scc for i in range(n_scc) [0] 0 subsum = [[0] for i in range(n_scc)] subsum [[0] for i in range(n_scc)] for i in range(n_scc):
    for j in scc[i]:
        subsum[i].append(subsum[i][-1] + c[j])
    for j in scc[i]:
        subsum[i].append(subsum[i][-1] + c[j]) i range(n_scc) range n_scc for j in scc[i]:
        subsum[i].append(subsum[i][-1] + c[j])
     j scc[i] scc i subsum[i].append(subsum[i][-1] + c[j]) [i].append [i] subsum i append subsum[i][-1] + c[j] subsum[i][-1] [i] subsum i -1 c[j] c j for j in scc[i]:
        subsum[i].append(subsum[i][-1] + c[j]) j scc[i] scc i subsum[i].append(subsum[i][-1] + c[j]) [i].append [i] subsum i append subsum[i][-1] + c[j] subsum[i][-1] [i] subsum i -1 c[j] c j def lister(k):
    for i in range(n_scc):
        l = len(scc[i])
        loop_score = max(0, subsum[i][l])
        for kk in range(1, min(k, l) + 1):
            base = loop_score * ((k - kk) // l)
            for j in range(kk, l + kk + 1):
                yield base + subsum[i][j] - subsum[i][j - kk] lister for i in range(n_scc):
        l = len(scc[i])
        loop_score = max(0, subsum[i][l])
        for kk in range(1, min(k, l) + 1):
            base = loop_score * ((k - kk) // l)
            for j in range(kk, l + kk + 1):
                yield base + subsum[i][j] - subsum[i][j - kk] i range(n_scc) range n_scc l = len(scc[i]) l len(scc[i]) len scc[i] scc i loop_score = max(0, subsum[i][l]) loop_score max(0, subsum[i][l]) max 0 subsum[i][l] [i] subsum i l for kk in range(1, min(k, l) + 1):
            base = loop_score * ((k - kk) // l)
            for j in range(kk, l + kk + 1):
                yield base + subsum[i][j] - subsum[i][j - kk] kk range(1, min(k, l) + 1) range 1 min(k, l) + 1 min(k, l) min k l 1 base = loop_score * ((k - kk) // l) base loop_score * ((k - kk) // l) loop_score (k - kk) // l k - kk k kk l for j in range(kk, l + kk + 1):
                yield base + subsum[i][j] - subsum[i][j - kk] j range(kk, l + kk + 1) range kk l + kk + 1 l + kk l kk 1 yield base + subsum[i][j] - subsum[i][j - kk] base + subsum[i][j] - subsum[i][j - kk] base + subsum[i][j] base subsum[i][j] [i] subsum i j subsum[i][j - kk] [i] subsum i j - kk j kk k k print(max(lister(k))) print max(lister(k)) max lister(k) lister k j = p[j] - 1 p[j] - 1 j p = list(map(int, input().split())) list(map(int, input().split())) p c = list(map(int, input().split())) list(map(int, input().split())) c k = map(int, input().split()) map(int, input().split()) k def lister(k):
    for i in range(n_scc):
        l = len(scc[i])
        loop_score = max(0, subsum[i][l])
        for kk in range(1, min(k, l) + 1):
            base = loop_score * ((k - kk) // l)
            for j in range(kk, l + kk + 1):
                yield base + subsum[i][j] - subsum[i][j - kk] def lister(k):
    for i in range(n_scc):
        l = len(scc[i])
        loop_score = max(0, subsum[i][l])
        for kk in range(1, min(k, l) + 1):
            base = loop_score * ((k - kk) // l)
            for j in range(kk, l + kk + 1):
                yield base + subsum[i][j] - subsum[i][j - kk] lister n_scc = len(scc) len(scc) n_scc n, k = map(int, input().split()) map(int, input().split()) n j = i i j scc = [] [] scc subsum = [[0] for i in range(n_scc)] [[0] for i in range(n_scc)] subsum visited = [False] * n [False] * n visited
# D - Moving Piece

n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
assert len(p) == len(c) == n

visited = [False] * n
scc = []
for i in range(n):
    if not visited[i]:
        scc.append([])
        j = i
        while not visited[j]:
            visited[j] = True
            scc[-1].append(j)
            j = p[j] - 1

n_scc = len(scc)
subsum = [[0] for i in range(n_scc)]
for i in range(n_scc):
    for j in scc[i]:
        subsum[i].append(subsum[i][-1] + c[j])
    for j in scc[i]:
        subsum[i].append(subsum[i][-1] + c[j])

def lister(k):
    for i in range(n_scc):
        l = len(scc[i])
        loop_score = max(0, subsum[i][l])
        for kk in range(1, min(k, l) + 1):
            base = loop_score * ((k - kk) // l)
            for j in range(kk, l + kk + 1):
                yield base + subsum[i][j] - subsum[i][j - kk]

print(max(lister(k)))
