#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define endl '\n'
typedef long long ll;
//	Table is the complete reverse of dp
//	Always think of the pattern (LCM, GCD)
//	Be careful of unsigned
//	Think of a solution that gets TLE and optimize it
//	Use binary search
//	Solve on paper first
int n, k, *p, *c, *dfn, *low, num, *pos;
vector<vector<int>> comp;
bool *vis;
ll rest(int& i, int& j, int k, ll sum, bool done)
{
	int r = j;
	ll tmp = sum;
	bool ok = 1;
	while(k--)
	{
		if(j == r)
		{
			if(ok)
				ok = 0;
			else
				break;
		}
		tmp += c[p[comp[i][j]]];
		//cout << ' ' << tmp;
		if(done)
			sum = max(sum, tmp);
		else
		{
			sum = tmp;
			done = 1;
		}
		j = (j + 1) % comp[i].size();
	}
	return sum;
}
void solve(int& i, int& j, int& k, ll& sum, bool& done)
{
	int r = j, idx = 1, l = 1;
	//cout << ' ' << p[comp[i][j]] + 1;
	ll cycle = c[p[comp[i][j]]], mx = cycle;
	j = (j + 1) % comp[i].size();
	while(j != r)
	{
		l++;
		//cout << ' ' << p[comp[i][j]] + 1;
		cycle += c[p[comp[i][j]]];
		if(mx < cycle)
		{
			mx = cycle;
			idx = l;
		}
		j = (j + 1) % comp[i].size();
	}
	//cout << " -> " << cycle << " //";
	if(cycle >= 0)
	{
		if(mx > cycle && k >= (int)comp[i].size() && k % (int)comp[i].size() < idx)
			sum = max(sum + cycle * (k / comp[i].size() - 1) + mx, cycle * (k / comp[i].size()) + rest(i, j, k % comp[i].size(), sum, 1));
		else
		{
			if(k >= (int)comp.size())
				done = 1;
			sum += cycle * (k / comp[i].size());
			k %= comp[i].size();
			sum = rest(i, j, k, sum, done);
		}
	}
	else
		sum = rest(i, j, k, sum, done);
	//cout << " --> " << sum << endl;
}
stack<int> st;
void tarjan(int& i)
{
	dfn[i] = low[i] = num++;
	vis[i] = 1;
	st.push(i);
	if(!~dfn[p[i]])
	{
		tarjan(p[i]);
		low[i] = min(low[i], low[p[i]]);
	}
	else if(vis[i])
		low[i] = min(low[i], dfn[p[i]]);
	if(dfn[i] == low[i])
	{
		int par = comp.size();
		comp.push_back(vector<int>());
		while(st.top() != i)
		{
			comp.back().push_back(st.top());
			vis[st.top()] = 0;
			pos[st.top()] = par;
			st.pop();
		}
		comp.back().push_back(st.top());
		vis[st.top()] = 0;
		pos[st.top()] = par;
		st.pop();
		reverse(comp.back().begin(), comp.back().end());
	}
}
int main()
{
    cin.tie(nullptr); ios_base::sync_with_stdio(0);
    cin >> n >> k;
    p = new int[n];
    for(int i = 0; i < n; i++)
    {
    	cin >> p[i];
    	p[i]--;
    }
    c = new int[n];
    for(int i = 0; i < n; i++)
    	cin >> c[i];
    vis = new bool[n];
    dfn = new int[n];
    low = new int[n];
    pos = new int[n];
    memset(vis, 0, n);
    memset(dfn, -1, n * 4);
    for(int i = 0; i < n; i++)
    	if(!~dfn[i])
    		tarjan(i);
    delete[] dfn;
    delete[] low;
    delete[] vis;
    ll ans = LLONG_MIN;
    for(int i = 0; i < n; i++)
	{
		int j = i, kk = k;
		ll sum = 0;
		bool done = 0;
		while(kk)
		{
			if(pos[j] == pos[p[j]])
			{
				//cout << 1;
				for(int o = 0; o < (int)comp[pos[j]].size(); o++)
					if(comp[pos[j]][o] == j)
					{
						//cout << j + 1 << ':';
						solve(pos[j], o, kk, sum, done);
						ans = max(ans, sum);
						break;
					}
				break;
			}
			else
			{
				//cout << 2;
				done = 1;
				sum += c[p[j]];
				ans = max(ans, sum);
				kk--;
				j = p[j];
			}
		}
	}
    cout << ans;
}


Measured memory_bytes: 4228