#include<bits/stdc++.h>
#define re register
#define int long long
using namespace std;
int n,a[100002],b,head[10002],cnt,ans,m,x,k,p[100002],val[5002],mx[5002];
inline int read(){
	re int t=0,f=0;re char v=getchar();
	while(v<'0')f|=(v=='-'),v=getchar();
	while(v>='0')t=(t<<3)+(t<<1)+v-48,v=getchar();
	return f?-t:t;
}
signed main(){
	n=read();k=read();
	for(re int i=1;i<=n;++i)p[i]=read();
	for(re int i=1;i<=n;++i)a[i]=read();mx[0]=-1e18;
	ans=-1e18;
	for(re int i=1;i<=n;++i){
		re int tot=0,nw=i;
		while(1){
			nw=p[nw];
			++tot;
			val[tot]=val[tot-1]+a[nw];
			mx[tot]=max(mx[tot-1],val[tot]);
			if(nw==i)break;
		}
		if(val[tot]>0)ans=max(ans,(k/tot)*val[tot]+((k%tot==0)?0ll:mx[k%tot]));
		if(k>=tot)ans=max(ans,(k/tot-1)*val[tot]+mx[tot]);
		ans=max(ans,mx[k>=tot?tot:k]);
	}
	printf("%lld",ans);
}
