#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <utility>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <iomanip>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 
//setbase - cout << setbase (16); cout << 100 << endl; Prints 64
//setfill -   cout << setfill ('x') << setw (5); cout << 77 << endl; prints xxx77
//setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
//cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val
 
using namespace std;
using namespace __gnu_pbds;
 	// find_by_order()  // order_of_key
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fi(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) f(i,0,n)
#define fd(i,a,b) for(ll i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define vi vector< int >
#define vl vector< ll >
#define ss second
#define ff first
#define ll long long
#define pii pair< int,int >
#define pll pair< ll,ll >
#define sz(a) a.size()
#define inff (1000*1000*1000+5)
#define all(a) a.begin(),a.end()
#define tri pair<int,pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define pqueue priority_queue< int >
#define pdqueue priority_queue< int,vi ,greater< int > >
#define flush fflush(stdout) 
#define primeDEN 727999983
#define SPEED ios_base::sync_with_stdio(false) ;cin.tie(NULL) ;cout.tie(NULL) ;
#define fp(i,a,n) for(ll i=n-1; i>a ;i--)
#define deb(x) cout << #x << " ---> " << x << endl;
#define X first
#define Y second
#define pia 3.1415926536
#define mod 1000000007
#define mod2 998244353

#define ml map<ll,ll>
#define mll map<pll,ll>
#define vvl vector<vl>
#define vvi vector<vi>
#define letsh(a,b) a<<b
#define rigsh(a,b) a>>b 
mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count()) ;
		bool sortbysec(const pair<int,int> &a, 
		      const pair<int,int> &b) 
		{ 
		    return (a.second < b.second); 
		} 	   
		
		  ll power(ll a,ll n)
		{
		ll ans=1;
		while(n)
		{
		if(n&1) ans=(ans*a)%mod;
		a=(a*a)%mod ;
		n=n>>1;
		 }
		 return ans;
		}
		ll power2(ll a,ll n)
		{
		ll ans =1 ;
		while(n)
		{
			if(n&1) ans =(ans*a) ;
			a = (a*a) ;
			n =n>>1 ;
		  }
		  return ans ;
		}
		ll lcm(ll a, ll b)
		{
		       return a / __gcd(a, b) * b;
		}
#define N 5006
        vl vis(N ,0) ;
        vvl g(N) ;vl sum(N,0);
        ll mxcu(vl arr, ll n, ll k) 
{ 
  
    ll sum = 0 ; 
    for (ll i = 0; i < k; i++) { 
        sum += arr[i]; 
    } 
  
    ll ans = sum; 
  
    for (ll i = k; i < n + k; i++) { 
        sum += arr[i % n] - arr[(i - k) % n]; 
  
        if (sum > ans) { 
            ans = sum;  
        } 
    } 
  
return ans ; 
} 
ll mxci(vector<ll> a, ll n) 
{ 
    if (n == 1) 
        return a[0]; 
   
    ll sum = 0; 
    for (ll i = 0; i < n; i++) { 
        sum += a[i]; 
    } 
    ll curr_max = a[0], max_so_far = a[0], curr_min = a[0], min_so_far = a[0];  
    for (ll i = 1; i < n; i++) { 
        curr_max = max(curr_max + a[i], a[i]); 
        max_so_far = max(max_so_far, curr_max);  
        curr_min = min(curr_min + a[i], a[i]); 
        min_so_far = min(min_so_far, curr_min); 
    } 
  
    if (min_so_far == sum) 
        return max_so_far;  
    return max(max_so_far, sum - min_so_far); 
} 
        void solve()
	    {
           ll n ,k;cin>>n>>k ;
		   vl p(n+1) ; f(i,0,n) cin>>p[i+1] ;
		   vl c(n+1)  ;f(i,0,n) cin>>c[i+1] ;
		    ll cnt =0 ;
		    g.resize(n+1) ;vis.resize(n+1,0) ;
		    f(i,0,n+1) g[i].clear() ;
		    sum.resize(n+1,0) ;
		   f(i,1,n+1)
		   {
		   	if(vis[i]) continue ;
		   	ll cur =p[i] ;
	        while(!vis[cur])	   	
		    {
		    	vis[cur] =1 ;g[cnt].pb(c[cur]) ;
				sum[cnt] +=c[cur] ;
				cur =p[cur] ;
			}
			cnt++ ;
		   }
		   ll ans = LLONG_MIN ;
		   f(i,0,cnt)
		   {
		   	ll si =g[i].size() ,mx = LLONG_MIN;
		   	ll cur =0 ,mx1 =LLONG_MIN ,cur1 =0 ,len =-1;
		   	vl a(si+3,0) ;
		   	f(j,1,si+1) a[j] = mxcu(g[i] ,si,j) ;
		      if(sum[i] <=0)
		      {
		       	f(j,1,min(k,si)+1) ans = max(ans,a[j]) ;
			  }
			  else
			  {
			  //	f(j,2,si+1) a[j] = max(a[j] ,a[j-1]) ;
			   f(j,0,min(k,si)+1)
			   {
			   	cur =(k-j)/si*sum[i] ;
			   	ans = max(cur+a[j] ,ans) ;
			   }
			  }
		   }
		   f(i,1,n+1) ans = max(ans,c[i]) ;
		   cout<<ans<<endl ;
		}
		
			  int main() 
	  	{
	     SPEED
	     ll t =1 ;
	     while(t--) solve() ;
	    }

Measured memory_bytes: 3988