import std.container;
import std.range;
import std.algorithm;
import std.array;
import std.string;
import std.conv;
import std.stdio;
import std.container;

void main() {
	long n, k;
	readf("%s %s\n", &n, &k);
	auto p = readln.chomp.split(' ').map!(s => s.to!long - 1).array;
	auto c = readln.chomp.split(' ').map!(to!long).array;
	auto cSum = c.sum;
	long best = long.min;
	long times;
	if (k < n * 2) {
		times = k;
		k = 0;
	} else {
		times = n * 2;
		k -= times;
	}

	foreach (i;0..n) {
		long position = i, score = 0;

		foreach (j;0..times) {
			score += c[position];
			if (best < score) {
				best = score;
			}
			position = p[position];
		}
	}

	if (cSum > 0) {
		best += cSum * (k / n);
	}
	writeln(best);
}
