import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N, K = MAP()
P = LIST()
C = LIST()

check = [0]*N 
score = []

i = 0
while 1:
	while i < N and check[i]:
		i += 1
	if i == N:
		break

	tmp = []
	while check[i] == 0:
		check[i] = 1
		i = P[i]-1
		tmp.append(C[i])
	score.append(tmp)

ans = -INF
for x in score:
	x_acc = list(accumulate([0] + x*3))
	n = len(x)

	if x_acc[n] <= 0:
		tmp_ans = 0
		l = min(K, n)
	else:
		tmp_ans = x_acc[n]*(max(0, K//n-1))
		l = min(K%n+n, K)
	tmp_max = -INF

	for i, j in combinations(range(3*n+1), 2):
		if j-i <= l:
			if tmp_max < x_acc[j]-x_acc[i]:
				tmp_max = x_acc[j]-x_acc[i]
	tmp_ans += tmp_max
	ans = max(ans, tmp_ans)

print(ans) import sys, re sys re from collections import deque, defaultdict, Counter from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby from operator import itemgetter, mul from copy import deepcopy from string import ascii_lowercase, ascii_uppercase, digits from bisect import bisect, bisect_left, insort, insort_left from heapq import heappush, heappop from functools import reduce, lru_cache def input(): return sys.stdin.readline().strip() input def INT(): return int(input()) INT def MAP(): return map(int, input().split()) MAP def LIST(): return list(map(int, input().split())) LIST def TUPLE(): return tuple(map(int, input().split())) TUPLE MAP() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) MAP() MAP def ZIP(n): return zip(*(MAP() for _ in range(n))) ZIP n n sys.setrecursionlimit(10 ** 9) sys.setrecursionlimit sys setrecursionlimit 10 ** 9 10 9 INF = float('inf') INF float('inf') float 'inf' mod = 10 ** 9 + 7 mod 10 ** 9 + 7 10 ** 9 10 9 7 from decimal import * N, K = MAP() N MAP() MAP K P = LIST() P LIST() LIST C = LIST() C LIST() LIST check = [0]*N check [0]*N [0] 0 N score = [] score [] i = 0 i 0 while 1:
	while i < N and check[i]:
		i += 1
	if i == N:
		break

	tmp = []
	while check[i] == 0:
		check[i] = 1
		i = P[i]-1
		tmp.append(C[i])
	score.append(tmp) 1 while i < N and check[i]:
		i += 1
	 i < N and check[i] i < N i N check[i] check i i += 1 i 1 if i == N:
		break

	 i == N i N break tmp = [] tmp [] while check[i] == 0:
		check[i] = 1
		i = P[i]-1
		tmp.append(C[i])
	 check[i] == 0 check[i] check i 0 check[i] = 1 check[i] check i 1 i = P[i]-1 i P[i]-1 P[i] P i 1 tmp.append(C[i]) tmp.append tmp append C[i] C i score.append(tmp) score.append score append tmp ans = -INF ans -INF INF for x in score:
	x_acc = list(accumulate([0] + x*3))
	n = len(x)

	if x_acc[n] <= 0:
		tmp_ans = 0
		l = min(K, n)
	else:
		tmp_ans = x_acc[n]*(max(0, K//n-1))
		l = min(K%n+n, K)
	tmp_max = -INF

	for i, j in combinations(range(3*n+1), 2):
		if j-i <= l:
			if tmp_max < x_acc[j]-x_acc[i]:
				tmp_max = x_acc[j]-x_acc[i]
	tmp_ans += tmp_max
	ans = max(ans, tmp_ans) x score x_acc = list(accumulate([0] + x*3)) x_acc list(accumulate([0] + x*3)) list accumulate([0] + x*3) accumulate [0] + x*3 [0] 0 x*3 x 3 n = len(x) n len(x) len x if x_acc[n] <= 0:
		tmp_ans = 0
		l = min(K, n)
	else:
		tmp_ans = x_acc[n]*(max(0, K//n-1))
		l = min(K%n+n, K)
	 x_acc[n] <= 0 x_acc[n] x_acc n 0 tmp_ans = 0 tmp_ans 0 l = min(K, n) l min(K, n) min K n tmp_ans = x_acc[n]*(max(0, K//n-1)) tmp_ans x_acc[n]*(max(0, K//n-1)) x_acc[n] x_acc n max(0, K//n-1) max 0 K//n-1 K//n K n 1 l = min(K%n+n, K) l min(K%n+n, K) min K%n+n K%n K n n K tmp_max = -INF tmp_max -INF INF for i, j in combinations(range(3*n+1), 2):
		if j-i <= l:
			if tmp_max < x_acc[j]-x_acc[i]:
				tmp_max = x_acc[j]-x_acc[i]
	 i j combinations(range(3*n+1), 2) combinations range(3*n+1) range 3*n+1 3*n 3 n 1 2 if j-i <= l:
			if tmp_max < x_acc[j]-x_acc[i]:
				tmp_max = x_acc[j]-x_acc[i]
	 j-i <= l j-i j i l if tmp_max < x_acc[j]-x_acc[i]:
				tmp_max = x_acc[j]-x_acc[i]
	 tmp_max < x_acc[j]-x_acc[i] tmp_max x_acc[j]-x_acc[i] x_acc[j] x_acc j x_acc[i] x_acc i tmp_max = x_acc[j]-x_acc[i] tmp_max x_acc[j]-x_acc[i] x_acc[j] x_acc j x_acc[i] x_acc i tmp_ans += tmp_max tmp_ans tmp_max ans = max(ans, tmp_ans) ans max(ans, tmp_ans) max ans tmp_ans print(ans) print ans def LIST(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) LIST def ZIP(n): return zip(*(MAP() for _ in range(n))) def ZIP(n): return zip(*(MAP() for _ in range(n))) ZIP K = MAP() MAP() K l = min(K, n) min(K, n) l N, K = MAP() MAP() N def TUPLE(): return tuple(map(int, input().split())) def TUPLE(): return tuple(map(int, input().split())) TUPLE n = len(x) len(x) n check = [0]*N [0]*N check i = 0 0 i score = [] [] score i = P[i]-1 P[i]-1 i P = LIST() LIST() P tmp = [] [] tmp x_acc = list(accumulate([0] + x*3)) list(accumulate([0] + x*3)) x_acc tmp_ans = x_acc[n]*(max(0, K//n-1)) x_acc[n]*(max(0, K//n-1)) tmp_ans def INT(): return int(input()) def INT(): return int(input()) INT ans = max(ans, tmp_ans) max(ans, tmp_ans) ans tmp_max = -INF -INF tmp_max mod = 10 ** 9 + 7 10 ** 9 + 7 mod i += 1 1 i def MAP(): return map(int, input().split()) def MAP(): return map(int, input().split()) MAP tmp_ans = 0 0 tmp_ans l = min(K%n+n, K) min(K%n+n, K) l tmp_ans += tmp_max tmp_max tmp_ans C = LIST() LIST() C INF = float('inf') float('inf') INF def input(): return sys.stdin.readline().strip() def input(): return sys.stdin.readline().strip() input ans = -INF -INF ans tmp_max = x_acc[j]-x_acc[i] x_acc[j]-x_acc[i] tmp_max
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N, K = MAP()
P = LIST()
C = LIST()

check = [0]*N 
score = []

i = 0
while 1:
	while i < N and check[i]:
		i += 1
	if i == N:
		break

	tmp = []
	while check[i] == 0:
		check[i] = 1
		i = P[i]-1
		tmp.append(C[i])
	score.append(tmp)

ans = -INF
for x in score:
	x_acc = list(accumulate([0] + x*3))
	n = len(x)

	if x_acc[n] <= 0:
		tmp_ans = 0
		l = min(K, n)
	else:
		tmp_ans = x_acc[n]*(max(0, K//n-1))
		l = min(K%n+n, K)
	tmp_max = -INF

	for i, j in combinations(range(3*n+1), 2):
		if j-i <= l:
			if tmp_max < x_acc[j]-x_acc[i]:
				tmp_max = x_acc[j]-x_acc[i]
	tmp_ans += tmp_max
	ans = max(ans, tmp_ans)

print(ans)
