N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

seen = []
roots = []
candidates = []

for i in range(N):
    if i+1 in seen:
        continue
    root = [i+1]
    seen.append(i+1)
    to = P[i]
    while(root[0] != to):
        root.append(to)
        seen.append(to)
        to = P[to-1]
    roots.append(root)
for root in roots:
    length = len(root)
    eachMoveBests = [None] * length
    for i in range(length):
        score = 0
        to = P[root[i]-1]
        for j in range(len(root)):
            score += C[to-1]
            to = P[to-1]
            if eachMoveBests[j] == None or score > eachMoveBests[j]:
                eachMoveBests[j] = score
    # 周回スコア
    aroundScore = eachMoveBests[len(eachMoveBests)-1]
    if length < K and aroundScore > 0:
        for i in range(len(eachMoveBests)):
            if K-i-1 % length == 0:
                candidates.append(aroundScore * ((K-i-1) // length))
            else:
                candidates.append(aroundScore * ((K-i-1) // length) + eachMoveBests[i])
    else:
        candidates.append(max(eachMoveBests[:K]))

print(max(candidates)) 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 seen = [] seen [] roots = [] roots [] candidates = [] candidates [] for i in range(N):
    if i+1 in seen:
        continue
    root = [i+1]
    seen.append(i+1)
    to = P[i]
    while(root[0] != to):
        root.append(to)
        seen.append(to)
        to = P[to-1]
    roots.append(root) i range(N) range N if i+1 in seen:
        continue
     i+1 in seen i+1 i 1 seen continue root = [i+1] root [i+1] i+1 i 1 seen.append(i+1) seen.append seen append i+1 i 1 to = P[i] to P[i] P i while(root[0] != to):
        root.append(to)
        seen.append(to)
        to = P[to-1]
     root[0] != to root[0] root 0 to root.append(to) root.append root append to seen.append(to) seen.append seen append to to = P[to-1] to P[to-1] P to-1 to 1 roots.append(root) roots.append roots append root for root in roots:
    length = len(root)
    eachMoveBests = [None] * length
    for i in range(length):
        score = 0
        to = P[root[i]-1]
        for j in range(len(root)):
            score += C[to-1]
            to = P[to-1]
            if eachMoveBests[j] == None or score > eachMoveBests[j]:
                eachMoveBests[j] = score
    # 周回スコア
    aroundScore = eachMoveBests[len(eachMoveBests)-1]
    if length < K and aroundScore > 0:
        for i in range(len(eachMoveBests)):
            if K-i-1 % length == 0:
                candidates.append(aroundScore * ((K-i-1) // length))
            else:
                candidates.append(aroundScore * ((K-i-1) // length) + eachMoveBests[i])
    else:
        candidates.append(max(eachMoveBests[:K])) root roots length = len(root) length len(root) len root eachMoveBests = [None] * length eachMoveBests [None] * length [None] None length for i in range(length):
        score = 0
        to = P[root[i]-1]
        for j in range(len(root)):
            score += C[to-1]
            to = P[to-1]
            if eachMoveBests[j] == None or score > eachMoveBests[j]:
                eachMoveBests[j] = score
    # 周回スコア
     i range(length) range length score = 0 score 0 to = P[root[i]-1] to P[root[i]-1] P root[i]-1 root[i] root i 1 for j in range(len(root)):
            score += C[to-1]
            to = P[to-1]
            if eachMoveBests[j] == None or score > eachMoveBests[j]:
                eachMoveBests[j] = score
    # 周回スコア
     j range(len(root)) range len(root) len root score += C[to-1] score C[to-1] C to-1 to 1 to = P[to-1] to P[to-1] P to-1 to 1 if eachMoveBests[j] == None or score > eachMoveBests[j]:
                eachMoveBests[j] = score
    # 周回スコア
     eachMoveBests[j] == None or score > eachMoveBests[j] eachMoveBests[j] == None eachMoveBests[j] eachMoveBests j None score > eachMoveBests[j] score eachMoveBests[j] eachMoveBests j eachMoveBests[j] = score eachMoveBests[j] eachMoveBests j score aroundScore = eachMoveBests[len(eachMoveBests)-1] aroundScore eachMoveBests[len(eachMoveBests)-1] eachMoveBests len(eachMoveBests)-1 len(eachMoveBests) len eachMoveBests 1 if length < K and aroundScore > 0:
        for i in range(len(eachMoveBests)):
            if K-i-1 % length == 0:
                candidates.append(aroundScore * ((K-i-1) // length))
            else:
                candidates.append(aroundScore * ((K-i-1) // length) + eachMoveBests[i])
    else:
        candidates.append(max(eachMoveBests[:K])) length < K and aroundScore > 0 length < K length K aroundScore > 0 aroundScore 0 for i in range(len(eachMoveBests)):
            if K-i-1 % length == 0:
                candidates.append(aroundScore * ((K-i-1) // length))
            else:
                candidates.append(aroundScore * ((K-i-1) // length) + eachMoveBests[i])
     i range(len(eachMoveBests)) range len(eachMoveBests) len eachMoveBests if K-i-1 % length == 0:
                candidates.append(aroundScore * ((K-i-1) // length))
            else:
                candidates.append(aroundScore * ((K-i-1) // length) + eachMoveBests[i])
     K-i-1 % length == 0 K-i-1 % length K-i K i 1 % length 1 length 0 candidates.append(aroundScore * ((K-i-1) // length)) candidates.append candidates append aroundScore * ((K-i-1) // length) aroundScore (K-i-1) // length K-i-1 K-i K i 1 length candidates.append(aroundScore * ((K-i-1) // length) + eachMoveBests[i]) candidates.append candidates append aroundScore * ((K-i-1) // length) + eachMoveBests[i] aroundScore * ((K-i-1) // length) aroundScore (K-i-1) // length K-i-1 K-i K i 1 length eachMoveBests[i] eachMoveBests i candidates.append(max(eachMoveBests[:K])) candidates.append candidates append max(eachMoveBests[:K]) max eachMoveBests[:K] eachMoveBests :K K print(max(candidates)) print max(candidates) max candidates seen = [] [] seen length = len(root) len(root) length eachMoveBests = [None] * length [None] * length eachMoveBests N, K = map(int, input().split()) map(int, input().split()) N score = 0 0 score to = P[root[i]-1] P[root[i]-1] to C = list(map(int, input().split())) list(map(int, input().split())) C roots = [] [] roots root = [i+1] [i+1] root to = P[to-1] P[to-1] to candidates = [] [] candidates aroundScore = eachMoveBests[len(eachMoveBests)-1] eachMoveBests[len(eachMoveBests)-1] aroundScore to = P[to-1] P[to-1] to K = map(int, input().split()) map(int, input().split()) K P = list(map(int, input().split())) list(map(int, input().split())) P score += C[to-1] C[to-1] score to = P[i] P[i] to
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

seen = []
roots = []
candidates = []

for i in range(N):
    if i+1 in seen:
        continue
    root = [i+1]
    seen.append(i+1)
    to = P[i]
    while(root[0] != to):
        root.append(to)
        seen.append(to)
        to = P[to-1]
    roots.append(root)
for root in roots:
    length = len(root)
    eachMoveBests = [None] * length
    for i in range(length):
        score = 0
        to = P[root[i]-1]
        for j in range(len(root)):
            score += C[to-1]
            to = P[to-1]
            if eachMoveBests[j] == None or score > eachMoveBests[j]:
                eachMoveBests[j] = score
    # 周回スコア
    aroundScore = eachMoveBests[len(eachMoveBests)-1]
    if length < K and aroundScore > 0:
        for i in range(len(eachMoveBests)):
            if K-i-1 % length == 0:
                candidates.append(aroundScore * ((K-i-1) // length))
            else:
                candidates.append(aroundScore * ((K-i-1) // length) + eachMoveBests[i])
    else:
        candidates.append(max(eachMoveBests[:K]))

print(max(candidates))
