from collections import defaultdict
from collections import deque
from collections import Counter
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n,k = readInts()
p = [0]+readInts()
c = [0]+readInts()

ans = -1*float("inf")
for i in range(1,n+1):
	j = p[i]
	t = [0]
	al = set()
	while j not in al:
		t.append(t[-1]+c[j])
		al.add(j)
		j = p[j]
	if t[-1]<=0:
		ans = max(ans, max(t[1:k+1]))
	else:
		kuri =(k-1)//(len(t)-1)
		s = kuri*t[-1]
		re = k -kuri*(len(t)-1)
		ans = max(ans, max(s,s+max(t[:re+1])))

print(ans) from collections import defaultdict from collections import deque from collections import Counter import math math def readInt():
	return int(input()) readInt return int(input()) int(input()) int input() input def readInts():
	return list(map(int, input().split())) readInts return list(map(int, input().split())) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split def readChar():
	return input() readChar return input() input() input def readChars():
	return input().split() readChars return input().split() input().split() ().split () input split n,k = readInts() n readInts() readInts k p = [0]+readInts() p [0]+readInts() [0] 0 readInts() readInts c = [0]+readInts() c [0]+readInts() [0] 0 readInts() readInts ans = -1*float("inf") ans -1*float("inf") -1 float("inf") float "inf" for i in range(1,n+1):
	j = p[i]
	t = [0]
	al = set()
	while j not in al:
		t.append(t[-1]+c[j])
		al.add(j)
		j = p[j]
	if t[-1]<=0:
		ans = max(ans, max(t[1:k+1]))
	else:
		kuri =(k-1)//(len(t)-1)
		s = kuri*t[-1]
		re = k -kuri*(len(t)-1)
		ans = max(ans, max(s,s+max(t[:re+1]))) i range(1,n+1) range 1 n+1 n 1 j = p[i] j p[i] p i t = [0] t [0] 0 al = set() al set() set while j not in al:
		t.append(t[-1]+c[j])
		al.add(j)
		j = p[j]
	 j not in al j al t.append(t[-1]+c[j]) t.append t append t[-1]+c[j] t[-1] t -1 c[j] c j al.add(j) al.add al add j j = p[j] j p[j] p j if t[-1]<=0:
		ans = max(ans, max(t[1:k+1]))
	else:
		kuri =(k-1)//(len(t)-1)
		s = kuri*t[-1]
		re = k -kuri*(len(t)-1)
		ans = max(ans, max(s,s+max(t[:re+1]))) t[-1]<=0 t[-1] t -1 0 ans = max(ans, max(t[1:k+1])) ans max(ans, max(t[1:k+1])) max ans max(t[1:k+1]) max t[1:k+1] t 1:k+1 1 k+1 k 1 kuri =(k-1)//(len(t)-1) kuri (k-1)//(len(t)-1) k-1 k 1 len(t)-1 len(t) len t 1 s = kuri*t[-1] s kuri*t[-1] kuri t[-1] t -1 re = k -kuri*(len(t)-1) re k -kuri*(len(t)-1) k kuri*(len(t)-1) kuri len(t)-1 len(t) len t 1 ans = max(ans, max(s,s+max(t[:re+1]))) ans max(ans, max(s,s+max(t[:re+1]))) max ans max(s,s+max(t[:re+1])) max s s+max(t[:re+1]) s max(t[:re+1]) max t[:re+1] t :re+1 re+1 re 1 print(ans) print ans ans = max(ans, max(s,s+max(t[:re+1]))) max(ans, max(s,s+max(t[:re+1]))) ans def readInt():
	return int(input()) def readInt():
	return int(input()) readInt def readChar():
	return input() def readChar():
	return input() readChar ans = max(ans, max(t[1:k+1])) max(ans, max(t[1:k+1])) ans s = kuri*t[-1] kuri*t[-1] s k = readInts() readInts() k ans = -1*float("inf") -1*float("inf") ans p = [0]+readInts() [0]+readInts() p c = [0]+readInts() [0]+readInts() c j = p[i] p[i] j def readChars():
	return input().split() def readChars():
	return input().split() readChars kuri =(k-1)//(len(t)-1) (k-1)//(len(t)-1) kuri def readInts():
	return list(map(int, input().split())) def readInts():
	return list(map(int, input().split())) readInts al = set() set() al re = k -kuri*(len(t)-1) k -kuri*(len(t)-1) re t = [0] [0] t j = p[j] p[j] j n,k = readInts() readInts() n
from collections import defaultdict
from collections import deque
from collections import Counter
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n,k = readInts()
p = [0]+readInts()
c = [0]+readInts()

ans = -1*float("inf")
for i in range(1,n+1):
	j = p[i]
	t = [0]
	al = set()
	while j not in al:
		t.append(t[-1]+c[j])
		al.add(j)
		j = p[j]
	if t[-1]<=0:
		ans = max(ans, max(t[1:k+1]))
	else:
		kuri =(k-1)//(len(t)-1)
		s = kuri*t[-1]
		re = k -kuri*(len(t)-1)
		ans = max(ans, max(s,s+max(t[:re+1])))

print(ans)
