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 bestList = new long[n];
	auto scoreList = new long[n];

	foreach (i;0..n) {
		long position = i;
		long cSum = 0;
		long cycleLength = 0;
		long score;
		scoreList[] = long.min;
		foreach (j;0..scoreList.length) {
			score += c[position];
			scoreList[j] = score;
			cSum += c[position];
			position = p[position];
			++cycleLength;
			if (cycleLength >= k) {
				break;
			}
			if (position == i) {
				break;
			}
		}

		long best;
		if (k < cycleLength) {
			best = scoreList[k - 1];
		} else {
			if (k % cycleLength == 0) {
				best = scoreList[0..cycleLength].reduce!max;
				if (cSum > 0) {
					best += cSum * (k / cycleLength - 1);
				}
			} else {
				best = scoreList[0..k % cycleLength].reduce!max;
				if (cSum > 0) {
					best += cSum * (k / cycleLength);
				}
			}
		}

		bestList[i] = best;
	}

	writeln(bestList.reduce!max);
}
