#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
template<class S, class T> inline S min_L(S a,T b){
  return a<=b?a:b;
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
template<class S, class T> inline S chmax(S &a, T b){
  if(a<b){
    a=b;
  }
  return a;
}
int N;
int K;
int P[5000];
int A[5000];
int go[35][5000];
long long ga[35][5000];
int main(){
  int i;
  int j;
  int k;
  int f;
  long long res = -4611686016279904256LL;
  long long tmp;
  rd(N);
  rd(K);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(P[Lj4PdHRW]);P[Lj4PdHRW] += (-1);
    }
  }
  {
    int e98WHCEY;
    for(e98WHCEY=(0);e98WHCEY<(N);e98WHCEY++){
      rd(A[e98WHCEY]);
    }
  }
  for(i=(0);i<(N);i++){
    go[0][i] = P[i];
    ga[0][i] = A[P[i]];
  }
  for(j=(1);j<(35);j++){
    for(i=(0);i<(N);i++){
      k = go[j-1][i];
      go[j][i] = go[j-1][k];
      ga[j][i] = ga[j-1][i] + ga[j-1][k];
    }
  }
  f =min_L(5100, K);
  for(i=(0);i<(N);i++){
    int V9aVTaxx;
    k = i;
    tmp = 0;
    for(V9aVTaxx=(0);V9aVTaxx<(f);V9aVTaxx++){
      k = P[k];
      tmp += A[k];
      chmax(res, tmp);
    }
  }
  f = K - 5050;
  if(f > 0){
    for(i=(0);i<(N);i++){
      int AlM5nNnR;
      k = i;
      tmp = 0;
      for(j=(0);j<(31);j++){
        if(f & (1<<j)){
          tmp += ga[j][k];
          k = go[j][k];
        }
      }
      for(AlM5nNnR=(0);AlM5nNnR<(5050);AlM5nNnR++){
        k = P[k];
        tmp += A[k];
        chmax(res, tmp);
      }
    }
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20200813-1 [beta]

// --- original code ---
// int N, K, P[5000], A[5000];
// int go[35][5000];
// ll ga[35][5000];
// {
//   int i, j, k, f;
//   ll res = -ll_inf, tmp;
//   rd(N,K,(P--)(N),A(N));
// 
//   rep(i,N){
//     go[0][i] = P[i];
//     ga[0][i] = A[P[i]];
//   }
//   rep(j,1,35) rep(i,N){
//     k = go[j-1][i];
//     go[j][i] = go[j-1][k];
//     ga[j][i] = ga[j-1][i] + ga[j-1][k];
//   }
// 
//   f = min(5100, K);
//   rep(i,N){
//     k = i;
//     tmp = 0;
//     rep(f){
//       k = P[k];
//       tmp += A[k];
//       res >?= tmp;
//     }
//   }
// 
//   f = K - 5050;
//   if(f > 0) rep(i,N){
//     k = i;
//     tmp = 0;
//     rep(j,31) if(f & (1<<j)){
//       tmp += ga[j][k];
//       k = go[j][k];
//     }
//     rep(5050){
//       k = P[k];
//       tmp += A[k];
//       res >?= tmp;
//     }
//   }
// 
//   wt(res);
// }


Measured memory_bytes: 5780