Language: C++

#include<bits/stdc++.h> 
typedef long long int ll;
using namespace std;
const ll P = 1000000007;

vector<ll> pre,vis;
void dfs(ll node, vector <ll> &p, vector<ll> &c){

	while(!vis[node]){
		vis[node] = 1;
		if(pre.size() == 0) pre.push_back(c[node]);
		else pre.push_back(pre.back() + c[node]);
		node = p[node];
	}

}

ll solve(){

	ll n,k;
	cin>>n>>k;

	vector<ll> p(n),c(n);
	for(ll i=0;i<n;++i) {cin>>p[i]; p[i]--;}
	for(ll i=0;i<n;++i) cin>>c[i];

	ll ans = -1e18;
	for(ll i=0;i<n;++i){
		pre.clear();
		vis.assign(n,0);
		dfs(i,p,c);

		ll maxa = -1e18;
		ll x = k/pre.size();
		if(x == 0){
			for(ll i=0;i<k;++i)
				maxa = max(maxa, pre[i]);
			ans = max(ans,maxa);
		}
		else{
			for(ll i=0;i<min( (ll)pre.size(), (ll)k - (ll)x*pre.size() ) ;++i){
				maxa = max(maxa , pre[i]);
			}
			ans = max(ans,maxa + x*pre.back());
			for(ll i=0;i<pre.size();++i){
				maxa = max(maxa , pre[i]);
			}
			ans = max(ans, maxa + (x-1)*pre.back());
		}

	}

	cout<<ans<<"\n";
	return 0;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

 	ll t = 1;
    // cin>>t;

    while(t--){
    	solve();
    }   

    return 0;
}	 

Compiler output:
a.cc: In function 'll solve()':
a.cc:41:41: error: no matching function for call to 'min(ll, long long unsigned int)'
   41 |                         for(ll i=0;i<min( (ll)pre.size(), (ll)k - (ll)x*pre.size() ) ;++i){
      |                                      ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:41:41: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'long long unsigned int')
   41 |                         for(ll i=0;i<min( (ll)pre.size(), (ll)k - (ll)x*pre.size() ) ;++i){
      |                                      ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:41:41: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   41 |                         for(ll i=0;i<min( (ll)pre.size(), (ll)k - (ll)x*pre.size() ) ;++i){
      |                                      ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~