#include<bits/stdc++.h>
#define int long long int
#define ll long long int
#define ld long double
#define getFaster ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define rep(i,init,n) for(int i=init;i<(int)n;i++)
#define rev(i,n,init) for(int i=(int)n;i>=init;i--)
#define MAXN 200005
#define MOD2 998244353
#define MOD1 1e9+7
#define f first
#define s second
#define endl '\n'
#define pii pair<int,int>
#define piii pair<pii,int>
#define precise(i) cout<<fixed<<setprecision(i)
#define codejam cout<<"Case #"<<ii+1<<": ";
#define impossible cout<<"IMPOSSIBLE"<<endl;
#define error(s) throw runtime_error(s)
// std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
// int myRand(int R) {
// 	int val=rng() % R;
//       assert(val>=0);
//       return val;
// }

using namespace std;
const long double PI = atan(1.0)*4;
const int64_t INF=3e18;
const int32_t LOG=21;
const int32_t MOD=MOD1;

vector<int> ord[5005];
bool check[5005];
int cost[5005];
int p[5005];
int groups=0;

void dfs(int i,int no)
{
     if(check[i])return; 
     ord[no].push_back(cost[i]); 
     check[i]=true;
     dfs(p[i],no);
}

int32_t main()
{ 
      getFaster;
      int tests=1; 
      //cin>>tests;
      rep(ii,0,tests)
      {
           groups=0;
           int n,k;
           cin>>n>>k;
           rep(i,1,n+1)cin>>p[i];
           rep(i,1,n+1)cin>>cost[i];
           rep(i,1,n+1)
           {
                 if(!check[i])
                 {
                      groups++; 
                      ord[groups].clear();
                      dfs(i,groups);
                 }
           }

           int ans=-INF;
           rep(i,1,groups+1)
           {
                 int sum=accumulate(ord[i].begin(),ord[i].end(),0LL);         
                 int siz=ord[i].size();
                 rep(j,0,siz)ord[i].push_back(ord[i][j]);
                 
                 
                 int div=k/siz;
                 int rem=k%siz;
                 int max1=-INF;
                 int tot=div*sum;
                 
                 if(div>0)
                 {
                       rep(j,0,siz)
                       {
                            int temp=0; 
                            rep(k,j,j+siz)
                            {
                                 temp+=ord[i][k];
                                 ans=max(ans,temp);
                            }
                       }
                 }

                 if(div>0) 
                 {
                       ans = max(ans, tot);
                       int tot1=(div-1)*sum;
                       if(div>1)
                       rep(j,0,siz)
                       {
                             int temp=tot1;
                             rep(k,j,j+siz)
                             {
                                   temp+=ord[i][k];
                                   ans=max(ans,temp);
                             }
                       }
                 }
                 

                 
                 
                 
                 rep(j,0,siz)
                 {
                       int temp=0;
                        rep(k,j,j+rem)
                        {
                            temp+=ord[i][k];
                            max1=max(max1,temp);
                        }     
                 }

                 tot+=max1;
                 ans=max(ans,tot);
           }
           cout<<ans<<endl;
      } 
      return 0;                         
}

Measured memory_bytes: 4012