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;

	long ans = long.min;
	auto s = new long[n];

	foreach (si;0..n) {
		auto x = si;
		long total;

		s.length = 0;
		for (;;) {
			x = p[x];
			s ~= c[x];
			total += c[x];
			if (x == si) {
				break;
			}
		}

		long now;
		foreach (long i, e;s) {
			now += e;
			if (i + 1 > k) {
				break;
			}
			if (total > 0) {
				long d = (k - (i + 1)) / s.length;
				ans = max(ans, now + total * d);
			} else {
				ans = max(ans, now);
			}
		}
	}

	writeln(ans);
}
