from sys import stdin
n,k = map(int,stdin.readline().split())
p = list(map(int,stdin.readline().split()))
c = list(map(int,stdin.readline().split()))

ans = -float("inf")
def helper(index):
    visited  = {index}
    looplen = 1
    looptotal = c[index]
    while p[index]-1 not in visited:
        nextindex = p[index] - 1
        looplen += 1
        looptotal += c[nextindex]
        visited.add(nextindex)
        index = nextindex
    return (looplen,looptotal)

for i in range(n):
    looplen,looptotal = helper(i)
    if looptotal >= 0:
        cur = looptotal*((k//looplen)-1) if k >= looplen else 0
        remain = looplen + (k%looplen) if k >= looplen else k
    else:
        cur = 0
        remain = looplen
    curmax = -float("inf")
    curindex = i
    for _ in range(remain):
        nextindex = p[curindex]-1
        cur += c[nextindex]
        curmax = max(curmax,cur)
        curindex = nextindex
    ans = max(ans,curmax)
print(ans) from sys import stdin n,k = map(int,stdin.readline().split()) n map(int,stdin.readline().split()) map int stdin.readline().split() stdin.readline().split stdin.readline() stdin.readline stdin readline split k p = list(map(int,stdin.readline().split())) p list(map(int,stdin.readline().split())) list map(int,stdin.readline().split()) map int stdin.readline().split() stdin.readline().split stdin.readline() stdin.readline stdin readline split c = list(map(int,stdin.readline().split())) c list(map(int,stdin.readline().split())) list map(int,stdin.readline().split()) map int stdin.readline().split() stdin.readline().split stdin.readline() stdin.readline stdin readline split ans = -float("inf") ans -float("inf") float("inf") float "inf" def helper(index):
    visited  = {index}
    looplen = 1
    looptotal = c[index]
    while p[index]-1 not in visited:
        nextindex = p[index] - 1
        looplen += 1
        looptotal += c[nextindex]
        visited.add(nextindex)
        index = nextindex
    return (looplen,looptotal) helper visited  = {index} visited {index} looplen = 1 looplen 1 looptotal = c[index] looptotal c[index] c index while p[index]-1 not in visited:
        nextindex = p[index] - 1
        looplen += 1
        looptotal += c[nextindex]
        visited.add(nextindex)
        index = nextindex
     p[index]-1 not in visited p[index]-1 p[index] p index 1 visited nextindex = p[index] - 1 nextindex p[index] - 1 p[index] p index 1 looplen += 1 looplen 1 looptotal += c[nextindex] looptotal c[nextindex] c nextindex visited.add(nextindex) visited.add visited add nextindex index = nextindex index nextindex return (looplen,looptotal) (looplen,looptotal) looplen looptotal index index for i in range(n):
    looplen,looptotal = helper(i)
    if looptotal >= 0:
        cur = looptotal*((k//looplen)-1) if k >= looplen else 0
        remain = looplen + (k%looplen) if k >= looplen else k
    else:
        cur = 0
        remain = looplen
    curmax = -float("inf")
    curindex = i
    for _ in range(remain):
        nextindex = p[curindex]-1
        cur += c[nextindex]
        curmax = max(curmax,cur)
        curindex = nextindex
    ans = max(ans,curmax) i range(n) range n looplen,looptotal = helper(i) looplen helper(i) helper i looptotal if looptotal >= 0:
        cur = looptotal*((k//looplen)-1) if k >= looplen else 0
        remain = looplen + (k%looplen) if k >= looplen else k
    else:
        cur = 0
        remain = looplen
     looptotal >= 0 looptotal 0 cur = looptotal*((k//looplen)-1) if k >= looplen else 0 cur looptotal*((k//looplen)-1) if k >= looplen else 0 k >= looplen k looplen looptotal*((k//looplen)-1) looptotal (k//looplen)-1 k//looplen k looplen 1 0 remain = looplen + (k%looplen) if k >= looplen else k remain looplen + (k%looplen) if k >= looplen else k k >= looplen k looplen looplen + (k%looplen) looplen k%looplen k looplen k cur = 0 cur 0 remain = looplen remain looplen curmax = -float("inf") curmax -float("inf") float("inf") float "inf" curindex = i curindex i for _ in range(remain):
        nextindex = p[curindex]-1
        cur += c[nextindex]
        curmax = max(curmax,cur)
        curindex = nextindex
     _ range(remain) range remain nextindex = p[curindex]-1 nextindex p[curindex]-1 p[curindex] p curindex 1 cur += c[nextindex] cur c[nextindex] c nextindex curmax = max(curmax,cur) curmax max(curmax,cur) max curmax cur curindex = nextindex curindex nextindex ans = max(ans,curmax) ans max(ans,curmax) max ans curmax print(ans) print ans def helper(index):
    visited  = {index}
    looplen = 1
    looptotal = c[index]
    while p[index]-1 not in visited:
        nextindex = p[index] - 1
        looplen += 1
        looptotal += c[nextindex]
        visited.add(nextindex)
        index = nextindex
    return (looplen,looptotal) def helper(index):
    visited  = {index}
    looplen = 1
    looptotal = c[index]
    while p[index]-1 not in visited:
        nextindex = p[index] - 1
        looplen += 1
        looptotal += c[nextindex]
        visited.add(nextindex)
        index = nextindex
    return (looplen,looptotal) helper n,k = map(int,stdin.readline().split()) map(int,stdin.readline().split()) n looptotal = helper(i) helper(i) looptotal ans = max(ans,curmax) max(ans,curmax) ans k = map(int,stdin.readline().split()) map(int,stdin.readline().split()) k cur = looptotal*((k//looplen)-1) if k >= looplen else 0 looptotal*((k//looplen)-1) if k >= looplen else 0 cur cur = 0 0 cur p = list(map(int,stdin.readline().split())) list(map(int,stdin.readline().split())) p curindex = nextindex nextindex curindex remain = looplen looplen remain looplen,looptotal = helper(i) helper(i) looplen remain = looplen + (k%looplen) if k >= looplen else k looplen + (k%looplen) if k >= looplen else k remain curmax = -float("inf") -float("inf") curmax nextindex = p[curindex]-1 p[curindex]-1 nextindex curmax = max(curmax,cur) max(curmax,cur) curmax c = list(map(int,stdin.readline().split())) list(map(int,stdin.readline().split())) c ans = -float("inf") -float("inf") ans cur += c[nextindex] c[nextindex] cur curindex = i i curindex
from sys import stdin
n,k = map(int,stdin.readline().split())
p = list(map(int,stdin.readline().split()))
c = list(map(int,stdin.readline().split()))

ans = -float("inf")
def helper(index):
    visited  = {index}
    looplen = 1
    looptotal = c[index]
    while p[index]-1 not in visited:
        nextindex = p[index] - 1
        looplen += 1
        looptotal += c[nextindex]
        visited.add(nextindex)
        index = nextindex
    return (looplen,looptotal)

for i in range(n):
    looplen,looptotal = helper(i)
    if looptotal >= 0:
        cur = looptotal*((k//looplen)-1) if k >= looplen else 0
        remain = looplen + (k%looplen) if k >= looplen else k
    else:
        cur = 0
        remain = looplen
    curmax = -float("inf")
    curindex = i
    for _ in range(remain):
        nextindex = p[curindex]-1
        cur += c[nextindex]
        curmax = max(curmax,cur)
        curindex = nextindex
    ans = max(ans,curmax)
print(ans)