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 = []

for i in range(N):
	if check[i]:
		continue

	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 + x))
	n = len(x)
	tmp_max = -INF

	for i, j in combinations(range(2*n+1), 2):
		if j-i <= min(K, n):
			if tmp_max < x_acc[j]-x_acc[i]:
				tmp_max = x_acc[j]-x_acc[i]
				cnt = j-i
	tmp_ans = -INF

	if 0 >= x_acc[n]:
		tmp_ans = tmp_max

	else:
		if cnt <= K%n:
			tmp_ans = x_acc[n]*(K//n) + tmp_max
		else:
			tmp_max2 = 0
			for i, j in combinations(range(2*n+1), 2):
				if j-i <= K%n:
					if tmp_max2 < x_acc[j]-x_acc[i]:
						tmp_max2 = x_acc[j]-x_acc[i]
			tmp_ans = max(x_acc[n]*(K//n-1)+tmp_max, x_acc[n]*(K//n) + tmp_max2)

	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 [] for i in range(N):
	if check[i]:
		continue

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

	 check[i] check i continue 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 + x))
	n = len(x)
	tmp_max = -INF

	for i, j in combinations(range(2*n+1), 2):
		if j-i <= min(K, n):
			if tmp_max < x_acc[j]-x_acc[i]:
				tmp_max = x_acc[j]-x_acc[i]
				cnt = j-i
	tmp_ans = -INF

	if 0 >= x_acc[n]:
		tmp_ans = tmp_max

	else:
		if cnt <= K%n:
			tmp_ans = x_acc[n]*(K//n) + tmp_max
		else:
			tmp_max2 = 0
			for i, j in combinations(range(2*n+1), 2):
				if j-i <= K%n:
					if tmp_max2 < x_acc[j]-x_acc[i]:
						tmp_max2 = x_acc[j]-x_acc[i]
			tmp_ans = max(x_acc[n]*(K//n-1)+tmp_max, x_acc[n]*(K//n) + tmp_max2)

	ans = max(ans, tmp_ans) x score x_acc = list(accumulate([0] + x + x)) x_acc list(accumulate([0] + x + x)) list accumulate([0] + x + x) accumulate [0] + x + x [0] + x [0] 0 x x n = len(x) n len(x) len x tmp_max = -INF tmp_max -INF INF for i, j in combinations(range(2*n+1), 2):
		if j-i <= min(K, n):
			if tmp_max < x_acc[j]-x_acc[i]:
				tmp_max = x_acc[j]-x_acc[i]
				cnt = j-i
	 i j combinations(range(2*n+1), 2) combinations range(2*n+1) range 2*n+1 2*n 2 n 1 2 if j-i <= min(K, n):
			if tmp_max < x_acc[j]-x_acc[i]:
				tmp_max = x_acc[j]-x_acc[i]
				cnt = j-i
	 j-i <= min(K, n) j-i j i min(K, n) min K n if tmp_max < x_acc[j]-x_acc[i]:
				tmp_max = x_acc[j]-x_acc[i]
				cnt = j-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 cnt = j-i cnt j-i j i tmp_ans = -INF tmp_ans -INF INF if 0 >= x_acc[n]:
		tmp_ans = tmp_max

	else:
		if cnt <= K%n:
			tmp_ans = x_acc[n]*(K//n) + tmp_max
		else:
			tmp_max2 = 0
			for i, j in combinations(range(2*n+1), 2):
				if j-i <= K%n:
					if tmp_max2 < x_acc[j]-x_acc[i]:
						tmp_max2 = x_acc[j]-x_acc[i]
			tmp_ans = max(x_acc[n]*(K//n-1)+tmp_max, x_acc[n]*(K//n) + tmp_max2)

	 0 >= x_acc[n] 0 x_acc[n] x_acc n tmp_ans = tmp_max tmp_ans tmp_max if cnt <= K%n:
			tmp_ans = x_acc[n]*(K//n) + tmp_max
		else:
			tmp_max2 = 0
			for i, j in combinations(range(2*n+1), 2):
				if j-i <= K%n:
					if tmp_max2 < x_acc[j]-x_acc[i]:
						tmp_max2 = x_acc[j]-x_acc[i]
			tmp_ans = max(x_acc[n]*(K//n-1)+tmp_max, x_acc[n]*(K//n) + tmp_max2)

	 cnt <= K%n cnt K%n K n tmp_ans = x_acc[n]*(K//n) + tmp_max tmp_ans x_acc[n]*(K//n) + tmp_max x_acc[n]*(K//n) x_acc[n] x_acc n K//n K n tmp_max tmp_max2 = 0 tmp_max2 0 for i, j in combinations(range(2*n+1), 2):
				if j-i <= K%n:
					if tmp_max2 < x_acc[j]-x_acc[i]:
						tmp_max2 = x_acc[j]-x_acc[i]
			 i j combinations(range(2*n+1), 2) combinations range(2*n+1) range 2*n+1 2*n 2 n 1 2 if j-i <= K%n:
					if tmp_max2 < x_acc[j]-x_acc[i]:
						tmp_max2 = x_acc[j]-x_acc[i]
			 j-i <= K%n j-i j i K%n K n if tmp_max2 < x_acc[j]-x_acc[i]:
						tmp_max2 = x_acc[j]-x_acc[i]
			 tmp_max2 < x_acc[j]-x_acc[i] tmp_max2 x_acc[j]-x_acc[i] x_acc[j] x_acc j x_acc[i] x_acc i tmp_max2 = x_acc[j]-x_acc[i] tmp_max2 x_acc[j]-x_acc[i] x_acc[j] x_acc j x_acc[i] x_acc i tmp_ans = max(x_acc[n]*(K//n-1)+tmp_max, x_acc[n]*(K//n) + tmp_max2) tmp_ans max(x_acc[n]*(K//n-1)+tmp_max, x_acc[n]*(K//n) + tmp_max2) max x_acc[n]*(K//n-1)+tmp_max x_acc[n]*(K//n-1) x_acc[n] x_acc n K//n-1 K//n K n 1 tmp_max x_acc[n]*(K//n) + tmp_max2 x_acc[n]*(K//n) x_acc[n] x_acc n K//n K n tmp_max2 ans = max(ans, tmp_ans) ans max(ans, tmp_ans) max ans tmp_ans print(ans) print ans P = LIST() LIST() P INF = float('inf') float('inf') INF mod = 10 ** 9 + 7 10 ** 9 + 7 mod tmp_max = -INF -INF tmp_max C = LIST() LIST() C tmp_ans = max(x_acc[n]*(K//n-1)+tmp_max, x_acc[n]*(K//n) + tmp_max2) max(x_acc[n]*(K//n-1)+tmp_max, x_acc[n]*(K//n) + tmp_max2) tmp_ans N, K = MAP() MAP() N tmp_ans = x_acc[n]*(K//n) + tmp_max x_acc[n]*(K//n) + tmp_max tmp_ans tmp = [] [] tmp ans = -INF -INF ans tmp_ans = tmp_max tmp_max tmp_ans i = P[i]-1 P[i]-1 i def LIST(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) LIST def TUPLE(): return tuple(map(int, input().split())) def TUPLE(): return tuple(map(int, input().split())) TUPLE tmp_max = x_acc[j]-x_acc[i] x_acc[j]-x_acc[i] tmp_max tmp_max2 = 0 0 tmp_max2 def MAP(): return map(int, input().split()) def MAP(): return map(int, input().split()) MAP ans = max(ans, tmp_ans) max(ans, tmp_ans) ans n = len(x) len(x) n tmp_max2 = x_acc[j]-x_acc[i] x_acc[j]-x_acc[i] tmp_max2 K = MAP() MAP() K tmp_ans = -INF -INF tmp_ans def input(): return sys.stdin.readline().strip() def input(): return sys.stdin.readline().strip() input x_acc = list(accumulate([0] + x + x)) list(accumulate([0] + x + x)) x_acc cnt = j-i j-i cnt def ZIP(n): return zip(*(MAP() for _ in range(n))) def ZIP(n): return zip(*(MAP() for _ in range(n))) ZIP score = [] [] score def INT(): return int(input()) def INT(): return int(input()) INT check = [0]*N [0]*N check
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 = []

for i in range(N):
	if check[i]:
		continue

	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 + x))
	n = len(x)
	tmp_max = -INF

	for i, j in combinations(range(2*n+1), 2):
		if j-i <= min(K, n):
			if tmp_max < x_acc[j]-x_acc[i]:
				tmp_max = x_acc[j]-x_acc[i]
				cnt = j-i
	tmp_ans = -INF

	if 0 >= x_acc[n]:
		tmp_ans = tmp_max

	else:
		if cnt <= K%n:
			tmp_ans = x_acc[n]*(K//n) + tmp_max
		else:
			tmp_max2 = 0
			for i, j in combinations(range(2*n+1), 2):
				if j-i <= K%n:
					if tmp_max2 < x_acc[j]-x_acc[i]:
						tmp_max2 = x_acc[j]-x_acc[i]
			tmp_ans = max(x_acc[n]*(K//n-1)+tmp_max, x_acc[n]*(K//n) + tmp_max2)

	ans = max(ans, tmp_ans)

print(ans)
