#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

vector<int>pes,color,ces,sz;
vector<ll>points;

void dfs(int u, int z){
	color[u]=z;
	sz[color[u]]++;
	points[color[u]]+=ces[u];
	int v = pes[u];
	if(color[v]==-1)dfs(v,z);
}


int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;ll k;
	cin>>n>>k;
	pes=ces=sz=vector<int>(n+1);
	color.resize(n+1,-1);
	points.resize(n+1);

	for(int i=1;i<=n;i++){
		cin>>pes[i];
	}
	for(int i=1;i<=n;i++){
		cin>>ces[i];
	}
	int z = 0;
	for(int i=1;i<=n;i++){
		if(color[i]==-1){
			dfs(i,++z);
		}
	}
	ll ans=-1e10;
	for(int i =1;i<=n;i++){
		int u=i;
		u=pes[u];
		ll tmp=0;
		for(int len=1;len<=sz[color[i]];len++){
			if(len>k)break;

			u=pes[u];
			tmp+=ces[u];
			ans=max(ans,tmp);
			ll foo=(k-len)/sz[color[i]];
			ans=max(ans,tmp+foo*points[color[i]]);
		}
	}
	cout<<ans;
	
	return 0;
}