#include "bits/stdc++.h"
using namespace std;
using ll = long long;

template <typename T>
void print_vec(const vector<T>& v, bool is_reverse=false, int num=0){
  if(num == 0) num = (int)v.size();
  if(is_reverse) for(int i=num-1; i>=0; i--){ cout<<v[i]; if(i!=0) cout<<" ";}
  else for(int i=0; i<num; i++){ cout<<v[i]; if(i!=num-1) cout<<" ";}
  cout << endl;
}

template <typename T>
void print_pairvec(const vector<T> &v, int num=0){
  if(num == 0) num = (int)v.size();
  cout << endl;
  for(int i=0; i<num; i++){ cout << v[i].first << " " << v[i].second << endl;}
}

template <typename T>
void print_vec2(const vector<vector<T>>& v){
  cout << endl; cout << "       ";
  for(int i=0; i<v[0].size(); i++) cout << i << "   "; 
  cout << endl;
	for(int i=0; i<v.size(); i++){
		cout << "i=" << i << ":   ";
		for(int j=0; j<v[i].size(); j++){
			if(v[i][j] == 0) cout << "\x1B[0m" << v[i][j] << "   ";
      else cout << "\x1B[31m" << v[i][j] << "   ";//https://stackoverrun.com/ja/q/12618775
		}
		cout << "\x1B[0m" << endl;
	}
}

using LP = pair<ll, ll>;  
int main(){
  ll N, K;
  cin >> N >> K;

  vector<ll> P(N), C(N);
  vector<ll> to(N);
  for(int i=0; i<N; i++){
    cin >> P[i];
    P[i]--;
    to[i] = P[i];
  }
  for(int i=0; i<N; i++) cin >> C[i];

  vector<vector<ll>> mazes(N);
  vector<LP> loops(N);// loops[i]: iから始めたときloopのはじまりとloop長さ
  vector<ll> get_score(N);//ループしてるとき,ループ開始から終わりまでの1周期分で得られるスコア
  for(int i=0; i<N; i++){
    vector<ll> node(N, 0);
    ll now = i;
    node[now] = 1;
    ll cnt = 0;// 始点iから ループの1周期分が終わるまでの回数
    while(true){
      now = to[now];
      if(node[now] == 1) break;
      node[now] = 1;
      cnt++;
    }
    ll init_loop = 0;
    ll j = i;
    ll flag = 0;// 
    while(true){
      if(j == now) flag++;
      if(flag == 0) init_loop++;
      j = to[j];
      if(flag == 1) get_score[i] += C[j];
      if(flag == 2) break;
    }

    loops[i] = LP(init_loop, (cnt - init_loop + 1));
  }
  

  ll const INF = 9*1e18 + 1;
  ll ans = -INF;
  for(int i=0; i<N; i++){
      ll score = 0;
      ll max_score = -INF;
      ll now = i;
      ll _K = K;
      for(int j=0; j<loops[i].first; i++){//ループに入るまでの計算
        now = to[now];
        score += C[now];
        max_score = max(max_score, score);
        _K--;
        if(_K == 0) break;
      }

      // 以下ループの処理

      if(get_score[i] <= 0){//ループしたらマイナスの場合は最大でも1ループしかしなくて良い
        for(int j=0; j<loops[i].second; j++){
          if(_K == 0) break;
          _K--;
          now = to[now];
          score += C[now];
          max_score = max(max_score, score);
        }
        ans = max(ans, max_score);
        continue;
      }

      // ll num = _K / loops[i].second;
      ll num = _K / loops[i].second  -1;// 最後に1周期分以上残すために-1しとく
      if(num < 0) num = 0;
      score += num * get_score[i];
      _K -= num * loops[i].second;

      //後1周期以上残ってるので，どこで止めるべきかを探す
      for(int j=0; j<_K; j++){
        now = to[now];
        score += C[now];
        max_score = max(max_score, score);
      }
      ans = max(ans, max_score);
  }
  // cout << " loops: "; print_pairvec(loops);
  // cout << " get_score: "; print_vec(get_score);
  cout << ans << endl;
  return 0;
}



Measured memory_bytes: 4028