#include<bits/stdc++.h>
using namespace std;
typedef int64_t ll;
#define rep(i,n) for(int i=1;i<=(int)(n);i++)
#define step b=p[b];d+=c[b];a=max(a,d);

int main(){
	ll n,k,a=(ll)-1e10;
	cin>>n>>k;
	ll p[n+1],c[n+1];
	rep(i,n)cin>>p[i];
	rep(i,n)cin>>c[i];
	rep(i,n){
		ll b=i,d=0,e=0;
		step;e++;
		if(k<n){while(e<k){step;e++;}}
		else{
			while(b!=i){step;e++;}
			d=d*(k/e-1);
			rep(j,e+k%e){step;}
        }
	}
	cout<<a<<endl;
}
