n, k = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
score = [0 for _ in range(n)]
loopn = [0 for _ in range(n)]
done = {i: True for i in range(n)}
cycles = []
nux = -1000000000000000
cycles_score = []
for start in range(n):
    if done[start]:
        done[start] = False
        cycle = [start]
        score = C[start]
        nex = P[start]-1
        while nex != start:
            done[nex] = False
            cycle.append(nex)
            score += C[nex]
            nex = P[nex]-1
        cycles.append(cycle)
        cycles_score.append(score)

for score, cycle in zip(cycles_score, cycles):
    l = len(cycle)
    acum = [0 for _ in range(2*l+1)]
    for i in range(2*l):
        acum[i+1] = acum[i]+C[cycle[i % l]]
    if score < 0:
        for r in range(l+1):
            for i in range(l):
                if r == 0:
                    continue
                if r <= k:
                    nux = max(nux, acum[i+r]-acum[i])
    else:
        for r in range(l+1):
            q = (k-r)//l
            for i in range(l):
                if r == 0 and q == 0:
                    continue
                if q <= 0 and r > k:
                    continue
                nux = max(nux, score*q+acum[i+r]-acum[i])


print(nux)

"""n, k = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

score = [0 for _ in range(n)]
loopn = [0 for _ in range(n)]
done = {i: True for i in range(n)}
for start in range(n):
    if done[start]:
        nex = -1
        j = 0
        tuuka = []
        nax = -float("inf")
        while nex != start:
            if nex == -1:
                nex = P[start]-1
            else:
                nex = P[nex]-1
            tuuka.append(nex)
            score[start] += C[nex]
            nax = max(nax, score[start])
            j += 1
        loopn[start] = j
        for i in tuuka:
            done[i] = False
            loopn[i] = j
            score[i] = score[start]
            nokori = k-(k//loopn[start])*loopn[start]

    if score[start] < 0:
        score[start] = 0
    else:
        score[start] = (k//loopn[start])*score[start]
        nokori = k-(k//loopn[start])*loopn[start]
        i = 0
        nnnnn = (k//loopn[start])
        nscore = 0
        nax = 0
        while i < nokori:
            nex = P[nex]-1
            nscore += C[nex]
            if nscore < 0:
                break
            nax = max(nax, nscore)
            i += 1
        if nax > 0:
            score[start] += nax


ans = max(score)
if ans <= 0:
    ans = max(C)
print(ans)
""" 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 0 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) 0 score = [0 for _ in range(n)] score [0 for _ in range(n)] 0 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) 0 loopn = [0 for _ in range(n)] loopn [0 for _ in range(n)] i: True for i in range(n) for i in range(n) i range(n) range n for i in range(n) i True done = {i: True for i in range(n)} done {i: True for i in range(n)} cycles = [] cycles [] nux = -1000000000000000 nux -1000000000000000 cycles_score = [] cycles_score [] for start in range(n):
    if done[start]:
        done[start] = False
        cycle = [start]
        score = C[start]
        nex = P[start]-1
        while nex != start:
            done[nex] = False
            cycle.append(nex)
            score += C[nex]
            nex = P[nex]-1
        cycles.append(cycle)
        cycles_score.append(score) start range(n) range n if done[start]:
        done[start] = False
        cycle = [start]
        score = C[start]
        nex = P[start]-1
        while nex != start:
            done[nex] = False
            cycle.append(nex)
            score += C[nex]
            nex = P[nex]-1
        cycles.append(cycle)
        cycles_score.append(score) done[start] done start done[start] = False done[start] done start False cycle = [start] cycle [start] start score = C[start] score C[start] C start nex = P[start]-1 nex P[start]-1 P[start] P start 1 while nex != start:
            done[nex] = False
            cycle.append(nex)
            score += C[nex]
            nex = P[nex]-1
         nex != start nex start done[nex] = False done[nex] done nex False cycle.append(nex) cycle.append cycle append nex score += C[nex] score C[nex] C nex nex = P[nex]-1 nex P[nex]-1 P[nex] P nex 1 cycles.append(cycle) cycles.append cycles append cycle cycles_score.append(score) cycles_score.append cycles_score append score for score, cycle in zip(cycles_score, cycles):
    l = len(cycle)
    acum = [0 for _ in range(2*l+1)]
    for i in range(2*l):
        acum[i+1] = acum[i]+C[cycle[i % l]]
    if score < 0:
        for r in range(l+1):
            for i in range(l):
                if r == 0:
                    continue
                if r <= k:
                    nux = max(nux, acum[i+r]-acum[i])
    else:
        for r in range(l+1):
            q = (k-r)//l
            for i in range(l):
                if r == 0 and q == 0:
                    continue
                if q <= 0 and r > k:
                    continue
                nux = max(nux, score*q+acum[i+r]-acum[i]) score cycle zip(cycles_score, cycles) zip cycles_score cycles l = len(cycle) l len(cycle) len cycle 0 for _ in range(2*l+1) for _ in range(2*l+1) _ range(2*l+1) range 2*l+1 2*l 2 l 1 for _ in range(2*l+1) 0 acum = [0 for _ in range(2*l+1)] acum [0 for _ in range(2*l+1)] for i in range(2*l):
        acum[i+1] = acum[i]+C[cycle[i % l]]
     i range(2*l) range 2*l 2 l acum[i+1] = acum[i]+C[cycle[i % l]] acum[i+1] acum i+1 i 1 acum[i]+C[cycle[i % l]] acum[i] acum i C[cycle[i % l]] C cycle[i % l] cycle i % l i l if score < 0:
        for r in range(l+1):
            for i in range(l):
                if r == 0:
                    continue
                if r <= k:
                    nux = max(nux, acum[i+r]-acum[i])
    else:
        for r in range(l+1):
            q = (k-r)//l
            for i in range(l):
                if r == 0 and q == 0:
                    continue
                if q <= 0 and r > k:
                    continue
                nux = max(nux, score*q+acum[i+r]-acum[i]) score < 0 score 0 for r in range(l+1):
            for i in range(l):
                if r == 0:
                    continue
                if r <= k:
                    nux = max(nux, acum[i+r]-acum[i])
     r range(l+1) range l+1 l 1 for i in range(l):
                if r == 0:
                    continue
                if r <= k:
                    nux = max(nux, acum[i+r]-acum[i])
     i range(l) range l if r == 0:
                    continue
                 r == 0 r 0 continue if r <= k:
                    nux = max(nux, acum[i+r]-acum[i])
     r <= k r k nux = max(nux, acum[i+r]-acum[i]) nux max(nux, acum[i+r]-acum[i]) max nux acum[i+r]-acum[i] acum[i+r] acum i+r i r acum[i] acum i for r in range(l+1):
            q = (k-r)//l
            for i in range(l):
                if r == 0 and q == 0:
                    continue
                if q <= 0 and r > k:
                    continue
                nux = max(nux, score*q+acum[i+r]-acum[i]) r range(l+1) range l+1 l 1 q = (k-r)//l q (k-r)//l k-r k r l for i in range(l):
                if r == 0 and q == 0:
                    continue
                if q <= 0 and r > k:
                    continue
                nux = max(nux, score*q+acum[i+r]-acum[i]) i range(l) range l if r == 0 and q == 0:
                    continue
                 r == 0 and q == 0 r == 0 r 0 q == 0 q 0 continue if q <= 0 and r > k:
                    continue
                 q <= 0 and r > k q <= 0 q 0 r > k r k continue nux = max(nux, score*q+acum[i+r]-acum[i]) nux max(nux, score*q+acum[i+r]-acum[i]) max nux score*q+acum[i+r]-acum[i] score*q+acum[i+r] score*q score q acum[i+r] acum i+r i r acum[i] acum i print(nux) print nux """n, k = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

score = [0 for _ in range(n)]
loopn = [0 for _ in range(n)]
done = {i: True for i in range(n)}
for start in range(n):
    if done[start]:
        nex = -1
        j = 0
        tuuka = []
        nax = -float("inf")
        while nex != start:
            if nex == -1:
                nex = P[start]-1
            else:
                nex = P[nex]-1
            tuuka.append(nex)
            score[start] += C[nex]
            nax = max(nax, score[start])
            j += 1
        loopn[start] = j
        for i in tuuka:
            done[i] = False
            loopn[i] = j
            score[i] = score[start]
            nokori = k-(k//loopn[start])*loopn[start]

    if score[start] < 0:
        score[start] = 0
    else:
        score[start] = (k//loopn[start])*score[start]
        nokori = k-(k//loopn[start])*loopn[start]
        i = 0
        nnnnn = (k//loopn[start])
        nscore = 0
        nax = 0
        while i < nokori:
            nex = P[nex]-1
            nscore += C[nex]
            if nscore < 0:
                break
            nax = max(nax, nscore)
            i += 1
        if nax > 0:
            score[start] += nax


ans = max(score)
if ans <= 0:
    ans = max(C)
print(ans)
""" loopn = [0 for _ in range(n)] [0 for _ in range(n)] loopn q = (k-r)//l (k-r)//l q score += C[nex] C[nex] score acum = [0 for _ in range(2*l+1)] [0 for _ in range(2*l+1)] acum cycles = [] [] cycles nex = P[start]-1 P[start]-1 nex done = {i: True for i in range(n)} {i: True for i in range(n)} done k = map(int, input().split()) map(int, input().split()) k P = list(map(int, input().split())) list(map(int, input().split())) P nux = -1000000000000000 -1000000000000000 nux score = C[start] C[start] score n, k = map(int, input().split()) map(int, input().split()) n l = len(cycle) len(cycle) l nux = max(nux, acum[i+r]-acum[i]) max(nux, acum[i+r]-acum[i]) nux cycle = [start] [start] cycle nex = P[nex]-1 P[nex]-1 nex nux = max(nux, score*q+acum[i+r]-acum[i]) max(nux, score*q+acum[i+r]-acum[i]) nux score = [0 for _ in range(n)] [0 for _ in range(n)] score C = list(map(int, input().split())) list(map(int, input().split())) C cycles_score = [] [] cycles_score
n, k = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
score = [0 for _ in range(n)]
loopn = [0 for _ in range(n)]
done = {i: True for i in range(n)}
cycles = []
nux = -1000000000000000
cycles_score = []
for start in range(n):
    if done[start]:
        done[start] = False
        cycle = [start]
        score = C[start]
        nex = P[start]-1
        while nex != start:
            done[nex] = False
            cycle.append(nex)
            score += C[nex]
            nex = P[nex]-1
        cycles.append(cycle)
        cycles_score.append(score)

for score, cycle in zip(cycles_score, cycles):
    l = len(cycle)
    acum = [0 for _ in range(2*l+1)]
    for i in range(2*l):
        acum[i+1] = acum[i]+C[cycle[i % l]]
    if score < 0:
        for r in range(l+1):
            for i in range(l):
                if r == 0:
                    continue
                if r <= k:
                    nux = max(nux, acum[i+r]-acum[i])
    else:
        for r in range(l+1):
            q = (k-r)//l
            for i in range(l):
                if r == 0 and q == 0:
                    continue
                if q <= 0 and r > k:
                    continue
                nux = max(nux, score*q+acum[i+r]-acum[i])


print(nux)

"""n, k = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

score = [0 for _ in range(n)]
loopn = [0 for _ in range(n)]
done = {i: True for i in range(n)}
for start in range(n):
    if done[start]:
        nex = -1
        j = 0
        tuuka = []
        nax = -float("inf")
        while nex != start:
            if nex == -1:
                nex = P[start]-1
            else:
                nex = P[nex]-1
            tuuka.append(nex)
            score[start] += C[nex]
            nax = max(nax, score[start])
            j += 1
        loopn[start] = j
        for i in tuuka:
            done[i] = False
            loopn[i] = j
            score[i] = score[start]
            nokori = k-(k//loopn[start])*loopn[start]

    if score[start] < 0:
        score[start] = 0
    else:
        score[start] = (k//loopn[start])*score[start]
        nokori = k-(k//loopn[start])*loopn[start]
        i = 0
        nnnnn = (k//loopn[start])
        nscore = 0
        nax = 0
        while i < nokori:
            nex = P[nex]-1
            nscore += C[nex]
            if nscore < 0:
                break
            nax = max(nax, nscore)
            i += 1
        if nax > 0:
            score[start] += nax


ans = max(score)
if ans <= 0:
    ans = max(C)
print(ans)
"""
