unittest
{
	assert( [ "5 2", "2 4 5 1 3", "3 4 -10 -8 8" ].solve == 8 );
	assert( [ "2 3", "2 1", "10 -7" ].solve == 13 );
	assert( [ "3 3", "3 1 2", "-1000 -2000 -3000" ].solve == -1000 );
	assert( [ "10 58", "9 1 6 7 8 4 3 2 10 5", "695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719" ].solve == 29507023469 );

	assert( [ "3 1", "2 3 1", "-1 -2 -3" ].solve == -1 );		// 追加ケース
}



import std.algorithm;
import std.conv;
import std.format;
import std.range;
import std.stdio;

void main()
{
	stdin.byLineCopy.solve.writeln;
}

auto solve( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto n = 0L;
	auto k = 0L;
	input.front.formattedRead!"%s %s"( n, k );
	input.popFront;
	auto ps = input.front.split.to!( long[] );
	ps[] -= 1;
	input.popFront;
	auto cs = input.front.split.to!( long[] );
	
	auto ans = long.min;
	foreach( i, p; ps )
	{
		auto t = 0L;
		auto c = 0L;
		
		// 1周するまでのスコアと移動回数を計算
		foreach( j; 0L .. k )
		{
			t += cs[ p ];
			ans = max( ans, t );
			c++;
			if( p == i ) break;
			p = ps[ p ];
		}
		
		// 最大までループしたあとにいくつか移動する場合のスコアを計算
		auto r = k / c - 1;
		t *= r;
		c *= r;
		foreach( j; c .. k )
		{
			p = ps[ p ];
			t += cs[ p ];
			ans = max( ans, t );
		}
	}
	return ans;
}
