#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;


typedef long long ll;
const ll INF = 1e18;


int main() {
	ll N, K;
	cin >> N >> K;

	vector<ll> P(N + 1), C(N + 1);
	for (int i = 1; i <= N; ++i) cin >> P[i];
	for (int i = 1; i <= N; ++i) cin >> C[i];

	ll ans = -INF;
	for (int s = 1; s <= N; ++s) {
		// find loop
		ll tot = 0, len = 0;
		int x = s;
		vector<bool> visited(N + 1, false);
		while (!visited[x]) {
			visited[x] = true;
			tot += C[x];
			len++;
			x = P[x];
		}

		// search max
		ll now = 0;
		for (int i = 1; i <= len; ++i) {
			x = P[x]; now += C[x];
			if (i > K) break;
			ll sc = now;
			if (tot > 0) sc += ((K - i) / len) * tot;
			ans = max(ans, sc);
		}
	}

	cout << ans << endl;

	return 0;
}
