#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
int main(){
  int N;
  long long K;
  cin>>N>>K;
  int P[5001];
  for(int i=1;i<=N;i++) cin>>P[i];
  long long C[5001];
  for(int i=1;i<=N;i++) cin>>C[i];
  long long answer=C[1];
  for(int i=1;i<=N;i++){
    int num=P[i];
    long long roop[5000];
    roop[0]=C[P[i]];
    long long key=roop[0];
    answer=max(answer,roop[0]);
    int j=1;
    while(j<K&&num!=i){
      num=P[num];
      key+=C[num];
      roop[j]=key;
      answer=max(answer,key);
      j++;
    }
    if(K>j&&roop[j-1]>0){
      long long a=roop[j-1]*((K/j)-1);
      for(int k=0;k<j;k++) answer=max(answer,a+roop[k]);
      for(int k=0;k<K%j;k++) answer=max(answer,a+roop[j-1]+roop[k]);
    }
  }
  cout<<answer<<endl;
  
  return 0;
}