Language: C++

#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
// typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define ll long long 
#define scn(n) scanf("%d",&n)
#define lscn(n) scanf("%lld",&n)
#define lpri(n) printf("%lld",n)
#define pri(n) printf("%d",n)
#define pln() printf("\n")
#define priln(n) printf("%d\n",n)
#define lpriln(n) printf("%lld\n",n)
#define rep(i,init,n) for(int i=init;i<n;i++)
#define pb push_back     
#define mp make_pair
#define F first
#define S second
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN      
const ll mod=1e9+7;
const int N=5e3+4;

vector<int> v[N];

vector<ll> cv;

int visit[N],val[N];

void dfs(int node)
{
	visit[node]=1;

	cv.pb(1LL*val[node]);

	for(int it: v[node])
	if(!visit[it])
	dfs(it);
}

int main()                  
{
	int n,k; scn(n); scn(k);

	rep(i,1,n+1)
	{
		int par; scn(par);

		v[par].pb(i); v[i].pb(par);
	}

	rep(i,1,n+1)
	scn(val[i]);

	ll ans=LLONG_MIN;

	rep(i,1,n+1)
	if(!visit[i])
	{
		cv.clear();

		dfs(i);

		vector<ll> vv=cv;

		ll sum=0;

		int sz=(int)cv.size();

		vector<ll> util(sz+1,LLONG_MIN);

		rep(i,1,sz)
		cv[i]+=cv[i-1];

		sum=cv[sz-1];

		rep(len,1,sz+1)
		{
			for(int i=0;i+len-1<sz;i++)
			{
				ll here=cv[i+len-1];

				if(i)
				here-=cv[i-1];

				util[len]=max(util[len],here);
			}
		}

		ll ans_here=1LL*sum*(k/sz);

		rep(len,1,sz+1)
		if(len<=k)
		{
			ll left=k-len; left/=sz;

			ll here=util[len];

			if(sum>0)
			here+=sum*left;

			ans_here=max(ans_here,here);
		}

		ans=max(ans,ans_here);
	}

	lpri(ans);

    return 0;
}

Compiler output:
a.cc: In function 'void dfs(int)':
a.cc:34:9: error: reference to 'visit' is ambiguous
   34 |         visit[node]=1;
      |         ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc:1:
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:30:5: note:                 'int visit [5004]'
   30 | int visit[N],val[N];
      |     ^~~~~
a.cc:39:13: error: reference to 'visit' is ambiguous
   39 |         if(!visit[it])
      |             ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:30:5: note:                 'int visit [5004]'
   30 | int visit[N],val[N];
      |     ^~~~~
a.cc: In function 'int main()':
a.cc:60:13: error: reference to 'visit' is ambiguous
   60 |         if(!visit[i])
      |             ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:30:5: note:                 'int visit [5004]'
   30 | int visit[N],val[N];
      |     ^~~~~