#include<algorithm>
#include<cstring>
#include<cstdio>
#define ll long long
#define qwq 6005
int n,k;
int c[qwq],p[qwq];
ll ans=-1e15-7;
ll maxn[qwq];
void dfs(int u){
	memset(maxn,-0x3f,sizeof(maxn));maxn[0]=0;
	int v=p[u];
	ll sum=0;
	int cnt=1;
	while(true){
		sum+=c[v];
		ans=std::max(ans,sum);
		maxn[cnt]=std::max(maxn[cnt-1],sum);
		if(v==u)break;
		v=p[v];
		++cnt;
		if(cnt>k)return ;
	}
	if(cnt<k&&sum>0){
		ans=std::max(ans,sum*(k/cnt)+maxn[k%cnt]);
		if(k/cnt-1>0)ans=std::max(ans,sum*(k/cnt-1)+maxn[cnt]);
	}
//	printf("%d %d\n",u,cnt);
	return ;
}
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;++i)scanf("%d",&p[i]);
	for(int i=1;i<=n;++i)scanf("%d",&c[i]);
	for(int i=1;i<=n;++i){
		dfs(i);
	}
	printf("%lld\n",ans);
	return 0;
}