Language: C++

#include <bits/stdc++.h>
using namespace std;

int64_t sol(int64_t N, int64_t K, vector<int> P, vector<int> C) {
	vector<int> done(N);

	int64_t ans = LLONG_MIN;
	
	for (int i = 0; i < N; ++i) {
		if (done[i]) continue;
		done[i] = 1;
		vector<int> cyc = {i};
		int64_t ccost = C[i];
		int at = i;
		while (P[at] != i) {
			at = P[at];
			ccost += C[at];
			cyc.push_back(at);
			done[at] = 1;
		}
		// cout << "here" << endl;
		// cout << "ee ";
		// for (int c : cyc) {
		// 	cout << c << " ";
		// }
		// cout << endl;

		int64_t mxextra = LLONG_MIN;
		for (int a = 0; a < cyc.size(); ++a) {
			int64_t extra = 0;
			int moves = 0;
			for (int b = (a+1)%cyc.size(); ; b=(b+1)%cyc.size()) {
				// cout << cyc.size() << " " << b << endl;
				++moves;
				if (moves > min(cyc.size(), K)) break;
				extra += C[cyc[b]];
				mxextra = max(mxextra, extra);
			}
		}
		// cout << "here" << endl;

		// cout << mxextra << endl;


		ans = max(ans, mxextra);

		int64_t tmpans = 0;

		int rem = K;
		if (ccost > 0) {
			tmpans += (K/cyc.size())*ccost;
			rem = K%cyc.size();
		}
		// cout << tmpans << " " << rem << endl;
		mxextra = 0;
		if (rem) {
			mxextra = LLONG_MIN;
			for (int a = 0; a < cyc.size(); ++a) {
				int64_t extra = 0;
				int moves = 0;
				for (int b = (a+1)%cyc.size(); b != a; b=(b+1)%cyc.size()) {
					++moves;
					if (moves > rem) break;
					extra += C[cyc[b]];
					mxextra = max(mxextra, extra);
				}
			}
		}
		ans = max(ans, tmpans+mxextra);
	}
	return ans;
}

int64_t brute(int64_t N, int64_t K, vector<int> P, vector<int> C) {
	int64_t ans = LLONG_MIN;
	for (int i = 0; i < N; ++i) {
		int64_t cst = 0;
		int at = i;
		for (int j = 0; j < K; ++j) {
			cst += C[P[at]];
			at = P[at];
			ans = max(ans, cst);
		}
	}
	return ans;
}

int main() {
	// default_random_engine re;
	// for (int i = 0; i < 100000; ++i) {
	// 	int N = 3;
	// 	int K = re()%(N*2)+1;
	// 	vector<int> p(N), c(N);
	// 	for (int j = 0; j < N; ++j) {
	// 		p[j] = j;
	// 		c[j] = re()%(8)-4;
	// 	}
	// 	shuffle(p.begin(), p.end(), re);
	// 	cout << N << " " << K << endl;
	// 	for (int i = 0; i < N; ++i) {
	// 		cout << p[i]+1 << " ";
	// 	}
	// 	cout << endl;
	// 	for (int i = 0; i < N; ++i) {
	// 		cout << c[i] << " ";
	// 	}
	// 	cout << endl;
	// 	// sol(N,K,p,c);
	// 	cout << "== " << sol(N,K,p,c) << " " << brute(N,K,p,c) << endl;
	// 	assert(sol(N,K,p,c)==brute(N,K,p,c));
	// }
	// return 0;

	int64_t N, K;
	cin >> N >> K;
	vector<int> P(N), C(N);
	for (int& e : P) {
		cin >> e;
		--e;
	}
	for (int& e : C) cin >> e;

	cout << sol(N,K,P,C) << endl;
}

Compiler output:
a.cc: In function 'int64_t sol(int64_t, int64_t, std::vector<int>, std::vector<int>)':
a.cc:35:48: error: no matching function for call to 'min(std::vector<int>::size_type, int64_t&)'
   35 |                                 if (moves > min(cyc.size(), K)) break;
      |                                             ~~~^~~~~~~~~~~~~~~
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:35:48: note:   deduced conflicting types for parameter 'const _Tp' ('long unsigned int' and 'int64_t' {aka 'long int'})
   35 |                                 if (moves > min(cyc.size(), K)) break;
      |                                             ~~~^~~~~~~~~~~~~~~
/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:35:48: note:   mismatched types 'std::initializer_list<_Tp>' and 'long unsigned int'
   35 |                                 if (moves > min(cyc.size(), K)) break;
      |                                             ~~~^~~~~~~~~~~~~~~