# -*- coding: utf-8 -*-
import sys 
from itertools import accumulate
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, K = map(int, readline().split())
P = [0] + list(map(int,readline().split()))
C = [0] + list(map(int,readline().split()))

ans = -10 ** 10
for i in range(1,N+1):
    l = 1
    j = P[i]
    s = C[j]
    ans = max(ans, s) #iを始まりとして
    while j != i:     # 1周期もしないときの最大値
        j = P[j]
        s += C[j]
        l += 1
        if K >= l:
            ans = max(ans, s)
    
    c = 1
    j = P[j]
    t = C[j]
    ans = max(ans, t + ((K - c) // l) * s) # 1回と周期分
    
    while j != i:
        j = P[j]
        t += C[j]
        c += 1
        if K >= c:
            ans = max(ans, t + ((K - c) // l) * s) # c回と周期分
print(ans) import sys sys from itertools import accumulate read = sys.stdin.buffer.read read sys.stdin.buffer.read sys.stdin.buffer sys.stdin sys stdin buffer read readline = sys.stdin.buffer.readline readline sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline readlines = sys.stdin.buffer.readlines readlines sys.stdin.buffer.readlines sys.stdin.buffer sys.stdin sys stdin buffer readlines N, K = map(int, readline().split()) N map(int, readline().split()) map int readline().split() ().split () readline split K P = [0] + list(map(int,readline().split())) P [0] + list(map(int,readline().split())) [0] 0 list(map(int,readline().split())) list map(int,readline().split()) map int readline().split() ().split () readline split C = [0] + list(map(int,readline().split())) C [0] + list(map(int,readline().split())) [0] 0 list(map(int,readline().split())) list map(int,readline().split()) map int readline().split() ().split () readline split ans = -10 ** 10 ans -10 ** 10 -10 10 for i in range(1,N+1):
    l = 1
    j = P[i]
    s = C[j]
    ans = max(ans, s) #iを始まりとして
    while j != i:     # 1周期もしないときの最大値
        j = P[j]
        s += C[j]
        l += 1
        if K >= l:
            ans = max(ans, s)
    
    c = 1
    j = P[j]
    t = C[j]
    ans = max(ans, t + ((K - c) // l) * s) # 1回と周期分
    
    while j != i:
        j = P[j]
        t += C[j]
        c += 1
        if K >= c:
            ans = max(ans, t + ((K - c) // l) * s) # c回と周期分 i range(1,N+1) range 1 N+1 N 1 l = 1 l 1 j = P[i] j P[i] P i s = C[j] s C[j] C j ans = max(ans, s) ans max(ans, s) max ans s while j != i:     # 1周期もしないときの最大値
        j = P[j]
        s += C[j]
        l += 1
        if K >= l:
            ans = max(ans, s)
    
     j != i j i j = P[j] j P[j] P j s += C[j] s C[j] C j l += 1 l 1 if K >= l:
            ans = max(ans, s)
    
     K >= l K l ans = max(ans, s) ans max(ans, s) max ans s c = 1 c 1 j = P[j] j P[j] P j t = C[j] t C[j] C j ans = max(ans, t + ((K - c) // l) * s) ans max(ans, t + ((K - c) // l) * s) max ans t + ((K - c) // l) * s t ((K - c) // l) * s (K - c) // l K - c K c l s while j != i:
        j = P[j]
        t += C[j]
        c += 1
        if K >= c:
            ans = max(ans, t + ((K - c) // l) * s) # c回と周期分 j != i j i j = P[j] j P[j] P j t += C[j] t C[j] C j c += 1 c 1 if K >= c:
            ans = max(ans, t + ((K - c) // l) * s) # c回と周期分 K >= c K c ans = max(ans, t + ((K - c) // l) * s) ans max(ans, t + ((K - c) // l) * s) max ans t + ((K - c) // l) * s t ((K - c) // l) * s (K - c) // l K - c K c l s print(ans) print ans t += C[j] C[j] t t = C[j] C[j] t readlines = sys.stdin.buffer.readlines sys.stdin.buffer.readlines readlines P = [0] + list(map(int,readline().split())) [0] + list(map(int,readline().split())) P readline = sys.stdin.buffer.readline sys.stdin.buffer.readline readline j = P[i] P[i] j s = C[j] C[j] s ans = max(ans, t + ((K - c) // l) * s) max(ans, t + ((K - c) // l) * s) ans c += 1 1 c ans = max(ans, s) max(ans, s) ans K = map(int, readline().split()) map(int, readline().split()) K s += C[j] C[j] s c = 1 1 c j = P[j] P[j] j ans = -10 ** 10 -10 ** 10 ans N, K = map(int, readline().split()) map(int, readline().split()) N l += 1 1 l ans = max(ans, s) max(ans, s) ans read = sys.stdin.buffer.read sys.stdin.buffer.read read j = P[j] P[j] j C = [0] + list(map(int,readline().split())) [0] + list(map(int,readline().split())) C l = 1 1 l j = P[j] P[j] j ans = max(ans, t + ((K - c) // l) * s) max(ans, t + ((K - c) // l) * s) ans
# -*- coding: utf-8 -*-
import sys 
from itertools import accumulate
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, K = map(int, readline().split())
P = [0] + list(map(int,readline().split()))
C = [0] + list(map(int,readline().split()))

ans = -10 ** 10
for i in range(1,N+1):
    l = 1
    j = P[i]
    s = C[j]
    ans = max(ans, s) #iを始まりとして
    while j != i:     # 1周期もしないときの最大値
        j = P[j]
        s += C[j]
        l += 1
        if K >= l:
            ans = max(ans, s)
    
    c = 1
    j = P[j]
    t = C[j]
    ans = max(ans, t + ((K - c) // l) * s) # 1回と周期分
    
    while j != i:
        j = P[j]
        t += C[j]
        c += 1
        if K >= c:
            ans = max(ans, t + ((K - c) // l) * s) # c回と周期分
print(ans)