#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <stack>
#include <tuple>
#include <cmath>
#include <iomanip>
#include <map>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <cctype>
#include <locale>
#define ll long long
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(),(a).end()
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
typedef pair<int,int> P;
typedef pair<long long,long long> Pll;
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
//s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ
//vector<vector<ll>> dp(n,vector<ll>(n))
//exist x map o setconst ll MOD = 1e9+7;return fac[n]*(finv[k]*finv[n-k] % MOD) % MOD;
//木の連結成分の個数は頂点数ー辺の数

signed main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int n; ll k;
    cin >> n >> k;
    vector<int> p(n);
    vector<ll> c(n);
    rep(i,n){
        cin >> p[i];
        p[i]--;
    }
    rep(i,n) cin >> c[i];
    
    vector<ll> ans(n,-1e9);
    vector<int> loop(n,0);
    rep(i,n){
        vector<bool> vis(n,0);
        int now=p[i];
        vis[i]=1;
        while(vis[now]==0){
            now=p[now];
        }
        loop[i]=now;
    }
    //loopsum
    vector<Pll> sum(n,{0,0});
    rep(i,n){
        int x=loop[i];
        if(sum[x].fi==0){
            ll res=c[x];
            int cnt=1;
            int nx=p[x];
            while(nx!=x){
                res+=c[nx];
                nx=p[nx];
                cnt++;
            }
            sum[x].fi=res;
            sum[x].se=cnt;
        }
    }
    rep(i,n){
        if(loop[i]==i){
            vector<ll> res(2*n+1);
            res[0]=0; int now=p[i];
            rep(j,2*n){
                res[j+1]=res[j]+c[now];
                now=p[now];
                if(j+1<=k) chmax(ans[i],res[j+1]);
            }
            ll resp=0;
            ll lp=k/sum[i].se;
            lp--;
            chmax(lp,0);
            resp+=lp*sum[i].fi;
            ll noko=k-lp*sum[i].se;
            rep(j,noko+1){
                if(j+lp>0) {
                    ll x=resp+res[j];
                    chmax(ans[i],x);
                }
            }
        }else{
            ll resp=0;
            ll kk=k;
            int nx=p[i];
            resp+=c[nx];
            chmax(ans[i],resp);
            kk--;
            while(nx!=loop[i]&&kk>0){
                nx=p[nx];
                resp+=c[nx]; chmax(ans[i],resp);
                kk--;
            }
            ll lp=kk/sum[nx].se;
            lp--;
            chmax(lp,0);
            resp+=lp*sum[nx].fi;
            ll noko=kk-lp*sum[nx].se;
            
            vector<ll> res(2*n+1);
            res[0]=0; int now=p[nx];
            rep(j,2*n){
                res[j+1]=res[j]+c[now];
                now=p[now];
            }
            rep(j,noko+1){
                chmax(ans[i],resp+res[j]);
            }
        }
    }
    ll out=-1e9;
    rep(i,n){
        chmax(out,ans[i]);
    }
    cout << out << endl;
}



Measured memory_bytes: 3820