#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF = 0x3f3f3f3f;
const ll mod  = 1e9 + 7;
const ll N = 5e3 + 5;

ll n,k,p[N],c[N];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin>>n>>k;
    for(int i=1;i<=n;i++) cin>>p[i];
    for(int i=1;i<=n;i++) cin>>c[i];
    ll ans=-1e18;
    for(int i=1;i<=n;i++){
    	ll s=c[i];
    	vector<ll> V;
    	V.emplace_back(s);
    	for(int j=p[i];j!=i;j=p[j]) s+=c[j],V.emplace_back(s);
    	int len=V.size();
    	for(int j=0;j<len;j++){
    		ll rest=k-j-1;
    		if(rest<0) break;
    		ans=max(ans,V[j]+(rest/len)*max(s,0ll));
    	}
    	cout<<'\n';
    }
    cout<<ans;
    return 0;
}