#if loc||local
#define _GLIBCXX_DEBUG
#endif

#include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(ll i=0;i<(n);++i)
using ll = int_fast64_t;
using pll = pair<ll,ll>;
constexpr ll INF = 1LL<<60;
constexpr ll MOD = 1e9+7;
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}return 0;}
#if loc||local
template<class T>void dump(T&& t){cerr<<t<<endl;}
template<class T,class... Ts> void dump(T&& h, Ts&&... t){cerr<<h<<", ";dump(forward<Ts>(t)...);}
#else
void dump(){}
template<class T,class... Ts> void dump(T&& h, Ts&&... t){}
#endif
template<class T> istream &operator>>(istream&is,vector<T>&v){for(auto &elemnt:v)is>>elemnt;return is;}
template<class T,class U> istream &operator>>(istream&is,pair<T,U>&p){is>>p.first>>p.second;return is;}
template<class T> ostream &operator<<(ostream& os,vector<T>const& v){for(auto const& vi:v)os<<vi<<" ";return os;}
template<class T,class U> ostream &operator<<(ostream& os,pair<T,U>const& p){os<<p.first<<","<<p.second;return os;}
template<class T>vector<T> vec(size_t a){return vector<T>(a);}
template<class T, class... Ts>auto vec(size_t a, Ts... ts){return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...));}

int main(){

  ll n,k;
  cin>>n>>k;
  vector<ll> p(n),c(n);
  cin>>p>>c;
  for(auto& pi:p)pi--;

  ll ans = -INF;
  rep(i,n){
    int cur = i;
    int _k = k;
    ll tmp = 0;
    vector<int> ord(n,-1);
    int od = 1;
    while(_k--){
      tmp += c[p[cur]];
      chmax(ans,tmp);
      ord[p[cur]] = od++;
      cur = p[cur];
      if(ord[p[cur]]!=-1)break;
    }
    if(_k==-1)continue;
    int loop_s = p[cur];
    ll loop_len = ord[cur]-ord[loop_s]+1;
    ll loop_num = (k-ord[cur])/loop_len;
    ll nokori = (k-ord[cur])%loop_len;
    ll ritoku = c[p[loop_s]];
    int v = p[loop_s];
    while(v!=loop_s){
      ritoku += c[p[v]];
      v = p[v];
    }
    dump(loop_len,loop_num,ritoku,nokori);
    if(ritoku>0)tmp += ritoku*loop_num;
    ll tmp2 = tmp-ritoku;
    chmax(ans,tmp);
    while(nokori--){
      tmp += c[p[cur]];
      cur = p[cur];
      chmax(ans,tmp);
    }
    v = loop_s;
    rep(_,loop_len){
      tmp2 += c[v];
      chmax(ans,tmp2);
      v = p[v];
    }
  }
  cout<<(ans)<<endl;

}
signed _main(){

  ll n,k;
  cin>>n>>k;
  vector<ll> p(n),c(n);
  cin>>p>>c;
  for(auto& pi:p)pi--;
  
  ll ans = -INF;
  
  auto simu = [&](int s){
    vector<int> ord(n,-1);
    vector<ll> point(n,0);
    int par = -1;
    int cur = s;
    int num = 0;
    ll _k = k;
    ll tmp = 0;
    while(_k--){
      if(ord[p[cur]]!=-1)break;
      ord[p[cur]] = num++;
      point[p[cur]] = c[p[cur]]+point[cur];
      tmp = point[p[cur]];
      chmax(ans,tmp);
      par = cur;
      cur = p[cur];
    }
    dump(s);dump(ord);dump(point);
    ll looplen = k-ord[cur];
    ll ritoku = 0;
    int now = p[cur];
    while(now!=cur){
      ritoku += c[now];
      now = p[now];
    }
    ritoku += c[now];
    dump(looplen,ritoku);
    if(ritoku>0){
      ll loopnum = (k-ord[par])/looplen;
      tmp += (loopnum-1)*ritoku;
      chmax(ans,tmp);
      ll nokori = (k-ord[par])%looplen;
      dump(nokori,ord[par],loopnum);
      while(nokori--){
        tmp += c[p[cur]];
        chmax(ans,tmp);
        cur = p[cur];
      }
    }
    chmax(ans,tmp);
  };
  rep(i,n)simu(i);
  cout<<(ans)<<endl;
  

}


Measured memory_bytes: 3700