(n, k), p, c = [[*map(int, i.split())] for i in open(0)]
 
def solve(x, t):
    visit = [0] * n
    visit[x] = 1
    loop = [0] * n
    count = 0
    ans = c[p[x] - 1]
    l = True
    sub = 0
    while True:
        x = p[x] - 1
        if l:
            if visit[x]:
                if loop[x]:
                    ln = sum(loop)
                    if t > ln:
                        sub += (t//ln -1)*count*(count>0)
                        t %= ln
                        t += ln
                    l = False
                count += c[x]
                loop[x] = 1
            visit[x] = 1
        sub += c[x]
        t -= 1
        ans = max(sub, ans)
        if t < 1:
            return ans
 
 
print(max(solve(i, k) for i in range(n))) [*map(int, i.split())] for i in open(0) for i in open(0) i open(0) open 0 for i in open(0) [*map(int, i.split())] *map(int, i.split()) map(int, i.split()) map int i.split() i.split i split (n, k), p, c = [[*map(int, i.split())] for i in open(0)] (n, k) n k [[*map(int, i.split())] for i in open(0)] p c def solve(x, t):
    visit = [0] * n
    visit[x] = 1
    loop = [0] * n
    count = 0
    ans = c[p[x] - 1]
    l = True
    sub = 0
    while True:
        x = p[x] - 1
        if l:
            if visit[x]:
                if loop[x]:
                    ln = sum(loop)
                    if t > ln:
                        sub += (t//ln -1)*count*(count>0)
                        t %= ln
                        t += ln
                    l = False
                count += c[x]
                loop[x] = 1
            visit[x] = 1
        sub += c[x]
        t -= 1
        ans = max(sub, ans)
        if t < 1:
            return ans
 
  solve visit = [0] * n visit [0] * n [0] 0 n visit[x] = 1 visit[x] visit x 1 loop = [0] * n loop [0] * n [0] 0 n count = 0 count 0 ans = c[p[x] - 1] ans c[p[x] - 1] c p[x] - 1 p[x] p x 1 l = True l True sub = 0 sub 0 while True:
        x = p[x] - 1
        if l:
            if visit[x]:
                if loop[x]:
                    ln = sum(loop)
                    if t > ln:
                        sub += (t//ln -1)*count*(count>0)
                        t %= ln
                        t += ln
                    l = False
                count += c[x]
                loop[x] = 1
            visit[x] = 1
        sub += c[x]
        t -= 1
        ans = max(sub, ans)
        if t < 1:
            return ans
 
  True x = p[x] - 1 x p[x] - 1 p[x] p x 1 if l:
            if visit[x]:
                if loop[x]:
                    ln = sum(loop)
                    if t > ln:
                        sub += (t//ln -1)*count*(count>0)
                        t %= ln
                        t += ln
                    l = False
                count += c[x]
                loop[x] = 1
            visit[x] = 1
         l if visit[x]:
                if loop[x]:
                    ln = sum(loop)
                    if t > ln:
                        sub += (t//ln -1)*count*(count>0)
                        t %= ln
                        t += ln
                    l = False
                count += c[x]
                loop[x] = 1
             visit[x] visit x if loop[x]:
                    ln = sum(loop)
                    if t > ln:
                        sub += (t//ln -1)*count*(count>0)
                        t %= ln
                        t += ln
                    l = False
                 loop[x] loop x ln = sum(loop) ln sum(loop) sum loop if t > ln:
                        sub += (t//ln -1)*count*(count>0)
                        t %= ln
                        t += ln
                     t > ln t ln sub += (t//ln -1)*count*(count>0) sub (t//ln -1)*count*(count>0) (t//ln -1)*count t//ln -1 t//ln t ln 1 count count>0 count 0 t %= ln t ln t += ln t ln l = False l False count += c[x] count c[x] c x loop[x] = 1 loop[x] loop x 1 visit[x] = 1 visit[x] visit x 1 sub += c[x] sub c[x] c x t -= 1 t 1 ans = max(sub, ans) ans max(sub, ans) max sub ans if t < 1:
            return ans
 
  t < 1 t 1 return ans ans x x t t print(max(solve(i, k) for i in range(n))) print max(solve(i, k) for i in range(n)) max solve(i, k) solve i k c = [[*map(int, i.split())] for i in open(0)] [[*map(int, i.split())] for i in open(0)] c p, c = [[*map(int, i.split())] for i in open(0)] [[*map(int, i.split())] for i in open(0)] p def solve(x, t):
    visit = [0] * n
    visit[x] = 1
    loop = [0] * n
    count = 0
    ans = c[p[x] - 1]
    l = True
    sub = 0
    while True:
        x = p[x] - 1
        if l:
            if visit[x]:
                if loop[x]:
                    ln = sum(loop)
                    if t > ln:
                        sub += (t//ln -1)*count*(count>0)
                        t %= ln
                        t += ln
                    l = False
                count += c[x]
                loop[x] = 1
            visit[x] = 1
        sub += c[x]
        t -= 1
        ans = max(sub, ans)
        if t < 1:
            return ans
 
  def solve(x, t):
    visit = [0] * n
    visit[x] = 1
    loop = [0] * n
    count = 0
    ans = c[p[x] - 1]
    l = True
    sub = 0
    while True:
        x = p[x] - 1
        if l:
            if visit[x]:
                if loop[x]:
                    ln = sum(loop)
                    if t > ln:
                        sub += (t//ln -1)*count*(count>0)
                        t %= ln
                        t += ln
                    l = False
                count += c[x]
                loop[x] = 1
            visit[x] = 1
        sub += c[x]
        t -= 1
        ans = max(sub, ans)
        if t < 1:
            return ans
 
  solve
(n, k), p, c = [[*map(int, i.split())] for i in open(0)]
 
def solve(x, t):
    visit = [0] * n
    visit[x] = 1
    loop = [0] * n
    count = 0
    ans = c[p[x] - 1]
    l = True
    sub = 0
    while True:
        x = p[x] - 1
        if l:
            if visit[x]:
                if loop[x]:
                    ln = sum(loop)
                    if t > ln:
                        sub += (t//ln -1)*count*(count>0)
                        t %= ln
                        t += ln
                    l = False
                count += c[x]
                loop[x] = 1
            visit[x] = 1
        sub += c[x]
        t -= 1
        ans = max(sub, ans)
        if t < 1:
            return ans
 
 
print(max(solve(i, k) for i in range(n)))