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

int main() {
  
  int N,K;
  cin >> N >> K;

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

  long long ans = -LONG_MAX;


  for(int i = 0;i < N;i++) {

    int cycle_num = 0;
    long long cycle_sum = 0;
    int idx = i;

    while(1) {
      cycle_num++;
      cycle_sum += C[idx];
      idx = P[idx];
      
      if(idx == i) break;
    }

    long long path = 0,cnt = 0;
    while(1) {
      cnt++;
      path += C[idx];

      if(cnt > K) break;

      int num = (K-cnt) / cycle_num;
      long long cost = path + max(0LL,cycle_sum)*num;
      ans = max(ans,cost);

      idx = P[idx];
      if(idx == i) break;

    }

  }

  cout << ans << endl;



}
