#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define int long long
#define double long double
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define mod 1000000007
#define ceil(a,b) (a+b-1)/b
#define endl "\n"
const int N=100005;
const int inf=1e18;
const double eps=1e-9;

#define oa(A) for(auto i:A)   cout<<i<<" ";cout<<endl;
#define o1(a) cout<<" "<<#a<<" : "<<a<<"\n"
#define o2(a,b) cout<<" "<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<"\n"
#define o3(a,b,c) cout<<" "<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<"\n"
#define o4(a,b,c,d) cout<<" "<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<" "<<#d<<" : "<<d<<"\n"

int power(int a,int b){
    int res=1;
    while(b>0){
        if(b&1){
        	res=res*a;
		}
        a=a*a;
        b>>=1;
    }
    return res;
}
int pow1(int a,int b){
    int res=1;
    while(b>0){
        if(b&1){
            res=res*a;
            res%=mod;
        }
        a=a*a;
        a%=mod;
        b>>=1;
    }
    return res;
}
int v[3001][3001][3];
signed main(){
	// ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	
	int n,k;
    cin>>n>>k;
    int p[n+1],c[n+1];
    for(int x=1;x<=n;x++){
        cin>>p[x];
    }
    for(int x=1;x<=n;x++){
        cin>>c[x];
    }
    int ans[n+1];
    for(int x=1;x<=n;x++){
        ans[x]=-1*inf;
    }
    for(int x=1;x<=n;x++){
        int vis[n+1]={0};
        int val[n+1];
        int i=x;
        int c1=1;
        int cmax=c[p[i]];
        int v=0;
        while(c1!=k+1&&vis[i]==0){
            // o2(x,i);
            val[i]=v;
            vis[i]=c1;
            i=p[i];
            c1++;
            v+=c[i];
            // val[i]=v;
            if(v>cmax){
                cmax=v;
            }
        }
        // o1(cmax);
        int tm=vis[i]-1;
        // o1(tm);
        if(c1==k+1){
            ans[x]=cmax;
            continue;
        }
        // o1(c1);
        for(int x=1;x<=n;x++){
            vis[x]=0;
        }
        int cl=0;
        int clsteps[n+2];
        clsteps[0]=0;
        int cyc_max=-1*inf;
        while(!vis[i]){
            // o2(x,i);
            cl++;
            vis[i]=1;
            i=p[i];
            clsteps[cl]=clsteps[cl-1]+c[i];
            cyc_max=max(cyc_max,clsteps[cl]);
        }
        // o1(cyc_max);
        // o1(cl);
        // o1(clsteps[cl]);
        // clsteps[cl+1]=clsteps[cl]+c[i];
        int tcm=k-tm;
        // o1(tcm);
        int cans=val[i];
        // o1(cans);
        if(clsteps[cl]>0){
            int mc=tcm/cl;
            // mc--;
            
            mc--;
            cans+=mc*(clsteps[cl])+cyc_max;
            cmax=max(cmax,cans);
            // o1(cmax);
            cans=val[i];
            mc++;
            cans+=mc*clsteps[cl];
            tcm-=mc*cl;
            cmax=max(cans,cmax);
            while(tcm!=0){
                i=p[i];
                cans+=c[i];
                cmax=max(cmax,cans);
                tcm--;
            }
        }
        ans[x]=max(cmax,cans);
        // cout<<ans[x]<<endl;
        // o2(x,ans[x]);
    }
    // oa(ans);
    int out=-1*inf;
    for(int x=1;x<=n;x++){
        out=max(out,ans[x]);
        // cout<<out;
    }
    cout<<out;






	
return 0;
}


Measured memory_bytes: 3824