import os
import sys
import math
import heapq
from decimal import *
from io import BytesIO, IOBase
from collections import defaultdict, deque

def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

n,k = rm()
p = rl()
p = [0]+p
c = rl()
c = [0]+c
b = [-1]*(n+1)
give=0
ans=-math.inf

for i in range(1,n+1):
    a=i
    lenn=1
    add=c[i]
    while p[a]!=i:
        add+=c[p[a]]
        lenn+=1
        a=p[a]
    if add<0:
        add=0
    addAgain=0
    a=i
    for j in range(lenn):
        if j>k:
            break
        addAgain+=c[a]
        ans = max(ans, addAgain+add*((k-j-1)//lenn))
        a = p[a]

print(ans) import os os import sys sys import math math import heapq heapq from decimal import * from io import BytesIO, IOBase from collections import defaultdict, deque def r():
    return int(input()) r return int(input()) int(input()) int input() input def rm():
    return map(int,input().split()) rm return map(int,input().split()) map(int,input().split()) map int input().split() ().split () input split def rl():
    return list(map(int,input().split())) rl return list(map(int,input().split())) list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split n,k = rm() n rm() rm k p = rl() p rl() rl p = [0]+p p [0]+p [0] 0 p c = rl() c rl() rl c = [0]+c c [0]+c [0] 0 c b = [-1]*(n+1) b [-1]*(n+1) [-1] -1 n+1 n 1 give=0 give 0 ans=-math.inf ans -math.inf math.inf math inf for i in range(1,n+1):
    a=i
    lenn=1
    add=c[i]
    while p[a]!=i:
        add+=c[p[a]]
        lenn+=1
        a=p[a]
    if add<0:
        add=0
    addAgain=0
    a=i
    for j in range(lenn):
        if j>k:
            break
        addAgain+=c[a]
        ans = max(ans, addAgain+add*((k-j-1)//lenn))
        a = p[a] i range(1,n+1) range 1 n+1 n 1 a=i a i lenn=1 lenn 1 add=c[i] add c[i] c i while p[a]!=i:
        add+=c[p[a]]
        lenn+=1
        a=p[a]
     p[a]!=i p[a] p a i add+=c[p[a]] add c[p[a]] c p[a] p a lenn+=1 lenn 1 a=p[a] a p[a] p a if add<0:
        add=0
     add<0 add 0 add=0 add 0 addAgain=0 addAgain 0 a=i a i for j in range(lenn):
        if j>k:
            break
        addAgain+=c[a]
        ans = max(ans, addAgain+add*((k-j-1)//lenn))
        a = p[a] j range(lenn) range lenn if j>k:
            break
         j>k j k break addAgain+=c[a] addAgain c[a] c a ans = max(ans, addAgain+add*((k-j-1)//lenn)) ans max(ans, addAgain+add*((k-j-1)//lenn)) max ans addAgain+add*((k-j-1)//lenn) addAgain add*((k-j-1)//lenn) add (k-j-1)//lenn k-j-1 k-j k j 1 lenn a = p[a] a p[a] p a print(ans) print ans addAgain+=c[a] c[a] addAgain b = [-1]*(n+1) [-1]*(n+1) b add=c[i] c[i] add a = p[a] p[a] a n,k = rm() rm() n def rl():
    return list(map(int,input().split())) def rl():
    return list(map(int,input().split())) rl c = rl() rl() c p = rl() rl() p ans = max(ans, addAgain+add*((k-j-1)//lenn)) max(ans, addAgain+add*((k-j-1)//lenn)) ans def rm():
    return map(int,input().split()) def rm():
    return map(int,input().split()) rm p = [0]+p [0]+p p def r():
    return int(input()) def r():
    return int(input()) r a=p[a] p[a] a lenn=1 1 lenn lenn+=1 1 lenn a=i i a a=i i a give=0 0 give add+=c[p[a]] c[p[a]] add add=0 0 add addAgain=0 0 addAgain c = [0]+c [0]+c c ans=-math.inf -math.inf ans k = rm() rm() k
import os
import sys
import math
import heapq
from decimal import *
from io import BytesIO, IOBase
from collections import defaultdict, deque

def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

n,k = rm()
p = rl()
p = [0]+p
c = rl()
c = [0]+c
b = [-1]*(n+1)
give=0
ans=-math.inf

for i in range(1,n+1):
    a=i
    lenn=1
    add=c[i]
    while p[a]!=i:
        add+=c[p[a]]
        lenn+=1
        a=p[a]
    if add<0:
        add=0
    addAgain=0
    a=i
    for j in range(lenn):
        if j>k:
            break
        addAgain+=c[a]
        ans = max(ans, addAgain+add*((k-j-1)//lenn))
        a = p[a]

print(ans)