#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pp pair<pair<ll, ll>,pair<ll, ll>>
#define pll pair<ll,ll>
#define ppll pair<ll,pll>
#define pdd pair<double,double>
#define vii vector<int>
#define vll vector<ll>
#define mat vector<vector<ll>>
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define eb emplace_back
#define fi first
#define sc second
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep2(i,a,b) for(ll i=a;i<b;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define all(x) x.begin(),x.end()
#define sz(x) (ll) (x).size()
#define pq priority_queue<ll>
#define pqg priority_queue<ll,vector<ll>,greater<ll>>
#define LB(v,x) (lower_bound(v.begin(),v.end(),x)-v.begin())
#define UB(v,x) (upper_bound(v.begin(),v.end(),x)-v.begin())
#define ERASE(v) sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end())
#define int ll
// #define ll int
using namespace std;
const ll INF = 1 << 29;
const ll LLINF = (1LL << 60LL);
const ll MOD = 1000000007;
const int MAX_V = 500;
// const ll mod = 998244353;
const ll MAX = 2100000;
const double pi = acos(-1);
const double eps = 1e-10;
ll dx[8] ={1,0,-1,0,1,-1,1,-1};
ll dy[8] ={0,1,0,-1,1,-1,-1,1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }





void solve(){
    ll n, k;
    cin>>n>>k;
    ll p[5010];
    rep(i,n){
        cin>>p[i];
        p[i]--;
    }
    ll c[5010];
    rep(i,n){
        cin>>c[i];
    }
    ll Ma=c[0];
    rep(i,n){
        chmax(Ma,c[i]);
    }
    if(Ma<=0){
        cout<<Ma<<endl;
        return;
    }

    ll ans=c[0];
    rep(i,n){
        chmax(ans,c[i]);
    }

    rep(i,n){

        // cout<<i<<endl;

        ll now=i;
        ll cnt=0;
        ll score[5010]={};

        while(true){
            ll next = p[now];
            // cout<<next<<" ";
            cnt++;
            score[cnt] = score[cnt-1] + c[next];
            now = next;
            if(now == i) break;
        }
        
        ll sum = score[1], rem = 0;
        // cout<<".."<<cnt<<endl;

        rep2(j,1,cnt+1){
            if(j > k) break;
            if(score[cnt] <= 0 ){
                chmax(ans, score[j]);
            }
            else{
                chmax(ans, score[j] + ( (k - j) / cnt) * score[cnt]);
            }
        }


        // if(k < cnt){
        //     sum = score[1];
        //     rep2(j,1,k+1){
        //         chmax(sum, score[j]);
        //     }
        // }
        // else if(score[cnt] > 0){
        //     sum = (k / cnt) * score[cnt];
        //     rem = k % cnt;
        //     ll M = 0;
        //     rep(j,rem+1){
        //         chmax(M, score[j]);
        //     }
        //     sum += M;
        // }
        // else{
        //     // cout<<"aa"<<endl;
        //     ll M = score[1];
        //     rep2(j,1,cnt+1){
        //         chmax(M, score[j]);
        //         // cout<<M<<"//";
        //     }
        //     sum = M;
        // }

        // cout<<i<<endl;
        // rep(j,cnt+2){
        //     cout<<score[j]<<" ";
        // }
        // cout<<endl;
        // cout<<sum<<endl;

        chmax(ans, sum);
    }

    cout<<ans<<endl;
}





signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);


    ll t=1;
    rep(i,t){
        solve(); 
    }


    return 0;
}

Measured memory_bytes: 3812