#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1000000007
using namespace std;

int main(){
  long n,k;
  cin >> n >> k;
  long p[n],c[n];
  rep(i,n){
    cin >> p[i];
    p[i]--;
  }
  rep(i,n)cin >> c[i];
  long ans = -INF;
  long tmp_score[n];
  rep(i,n)tmp_score[i] = 0;
  rep(i,n){
    long tmp_max = -INF, tmp = 0, now = i, iter = 0;
    while(1){
      if((iter>0 && now==i) || iter >= k)break;
      now = p[now];
      tmp += c[now];
      tmp_score[iter] = tmp;
      if(tmp > tmp_max)tmp_max = tmp;
      iter++;
    }
    if(tmp_max > ans)ans = tmp_max;
    if(tmp > 0 && iter < k){
      rep(j,iter){
        long score = tmp_score[j] + tmp*(k/iter - 1);
        if(k%iter >= j+1)score += tmp;
        if(ans < score)ans = score;
      }
    }
  }
  cout << ans << endl;
  return 0;
}