#include<bits/stdc++.h>
#define INF 1000000000000000
using namespace std;
template <typename T> T Max(T x,T y){return x>y?x:y;}
int n,k,c[5005],p[5005];
short g[5005][5005];
long long ans=-INF,f[5005][5005];
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++)
	{
		g[i][0]=i;
		f[i][0]=0;
		for(int j=1;j<=min(n,k);j++)
		{
			g[i][j]=p[g[i][j-1]];
			f[i][j]=f[i][j-1]+(long long)c[g[i][j]];
			ans=Max(ans,f[i][j]);
		}
	}
	
	if(k<=n)
	{
		cout<<ans<<endl;
		return 0;
	}
	for(int i=1;i<=n;i++)
	{
		int j=1;
		while(g[i][j]!=i) j++;
		for(int l=0;l<=k%j;l++)
			ans=Max(ans,(k/j)*f[i][j]+f[i][l]);
		for(int l=0;l<j;l++)
			ans=Max(ans,(k/j-1)*f[i][j]+f[i][l]);
	}
	cout<<ans<<endl;
	return 0;
}