#pragma comment(linker,"/stack:16777216")
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<functional>
#include<utility>
#include<algorithm>
#include<sstream>
#include<cstring>
#include<cmath>
#include<cctype>
#include<exception>
#include<climits>
#include<cfloat>
#include<iomanip>
#include<bitset>
#include<streambuf>
#include<cstdlib>
#include<ctime>
//#include"functions.h"
//#include"highprecision.h"
#define pb push_back
#define pf push_front
#define ppb pop_back
#define mp make_pair
#define np next_permutation
#define fi first
#define sc second
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vector<int> >
#define vpi vector<pair<int,int> >
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pff pair<float,float>
#define pdd pair<double,double>
#define psi pair<string,int>
#define pis pair<int,string>
#define ppi pair<pii,int>
#define pdi pair<pii,pii>
#define LN 84757
#define INF 1e9
#define LLINF 1e18
#define MAXINT 0x7fffffff
#define MAXLL 0x7fffffffffffffff
#define EPS 1e-5
#define DEPS 1e-9
#define LDEPS 1e-13
#define MOD 1000000007
#define E 2.7182818284
#define PI 3.1415926535
#define rep(a,b) for(int a=0;a<b;a++)
#define rep(a,b) for(int a=0;a<b;a++)
#define forn(a,n) for(int a=1;a<=n;a++)
#define forr(a,n) for(int a=n;a>=1;a--)
#define repn(a,n,p) for(int a=0;a<n;a+=p)
#define repr(a,n) for(int a=n-1;a>=0;a--)
#define foreach(a,b) for(__typeof(b.begin())a=b.begin();a!=b.end();a++)
#define fillemp(a) memset(a,0,sizeof(a))
#define fillinf(a) memset(a,0x3f,sizeof(a))
#define frestream freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define stdf(a,b) freopen(a,"r",stdin);freopen(b,"w",stdout);
#define inputf(a) freopen(a,"r",stdin);
#define outputf(a) freopen(a,"w",stdout);
#define sz(a) a.size()
#define all(a) a.begin(),a.end()
#define MOD 1000000007
typedef long long ll;
typedef long double ld;
using namespace std;

int n,k,cur;
ll p[5010],c[5010],low[5010],sum[5010],dfn[5010],siz[5010],cycle[5010];
bool vis[5010];
stack<int> stk;
void dfs(int v){
	vis[v]=1;
	low[v]=cur;
	dfn[v]=cur;
	cur++;
	int to=p[v];
	stk.push(v);
	if(vis[to]){
		low[v]=min(low[v],dfn[to]);
	}
	else{
		dfs(to);
		low[v]=min(low[v],low[to]);
	}
	if(low[v]==dfn[v]){
		while(stk.top()!=v){
			int x=stk.top();
			stk.pop();
			cycle[x]=v;
			sum[v]+=c[x];
			siz[v]++;
		}
		cycle[v]=v;
		sum[v]+=c[v];
		siz[v]++;
	}
}
int main(int argc,char** argv){
	cin>>n>>k;
	rep(i,n){
		cycle[i]=-1;
		cin>>p[i];
		p[i]--;
	}
	rep(i,n){
		cin>>c[i];
	}
	rep(i,n){
		if(!vis[i]){
			dfs(i);
		}
	}
	/*
	rep(i,n){
		cout<<cycle[i]<<" "<<sum[i]<<endl;
	}
	*/
	ll res=-LLINF;
	rep(i,n){
		ll ans=-LLINF;
		//enumerate the starting position
		ll pos=i,cyc=-1,pre=0,cnt=0;
		while(pos!=cyc&&cnt<=k){
			pre+=c[pos];
			//cout<<i<<" "<<pos<<" "<<pre<<" "<<cyc<<endl;
			if(pos!=i)ans=max(ans,pre);
			if(cycle[pos]>=0&&cyc<0){
				cyc=pos;
			}
			if(sum[cycle[cyc]]>0){
				//cout<<cycle[cyc]<<endl;
				//consider going around the cycle
				//cout<<i<<" "<<cyc<<" "<<k<<" "<<cnt<<" "<<pre<<endl;
				//cout<<pre+sum[cycle[cyc]]*((k-cnt)/siz[cycle[cyc]])<<endl;
				ans=max(ans,pre+sum[cycle[cyc]]*((k-cnt)/siz[cycle[cyc]]));
			}
			pos=p[pos];
			cnt++;
		}
		//cout<<i<<" ans:"<<ans<<endl;
		ans-=c[i];
		//cout<<i<<" ans:"<<ans<<endl;
		res=max(res,ans);
	}
	cout<<res<<endl;
	return 0;
}
/*
3 4
3 1 2
-1000 -2000 -3000
*/

Measured memory_bytes: 4324