Language: C++

#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
#include <random>
using namespace std;
#define ll long long
#define fr first
#define sc second
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()

int N, K;
int P[5500];
int C[5500];
vector<vector<ll>> cycle;

ll f(vector<ll>& v) {
	ll s = 0;
	vector<ll> tmp = v;
	for (int i : v) tmp.push_back(i);
	vector<ll> psum(tmp.size() + 1);
	for (int i = 0; i < tmp.size(); i++)
		psum[i + 1] = psum[i] + C[tmp[i]];

	ll ret = -2e18;
	for (int i = 0; i < v.size(); i++)
		for (int j = i; j < i + v.size(); j++) {
			int len = j - i + 1;
			if (len > K) continue;
			ret = max(ret, psum[j + 1] - psum[i] + (K - len) / v.size() * max(0LL, psum[v.size()]));
		}
	return ret;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> N >> K;
	for (int i = 0; i < N; i++) cin >> P[i];
	for (int i = 0; i < N; i++) cin >> C[i];

	bool visited[5500] = { 0 };
	for (int i = 0; i < N; i++) {
		vector<ll> tmp;
		int pos = i + 1;
		if (!visited[pos]) {
			tmp.push_back(pos - 1);
			visited[pos] = 1;
			while (true) {
				pos = P[pos - 1];
				if (visited[pos]) break;
				visited[pos] = 1;
				tmp.push_back(pos - 1);
			}
			cycle.push_back(tmp);
			tmp.clear();
		}
	}

	ll ans = -2e18;
	for (auto tmp : cycle) {
		ans = max(ans, f(tmp));
	}
	cout << ans << '\n';
	return 0;
}

Compiler output:
a.cc: In function 'long long int f(std::vector<long long int>&)':
a.cc:30:34: error: no matching function for call to 'max(long long int&, long long unsigned int)'
   30 |                         ret = max(ret, psum[j + 1] - psum[i] + (K - len) / v.size() * max(0LL, psum[v.size()]));
      |                               ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:30:34: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'long long unsigned int')
   30 |                         ret = max(ret, psum[j + 1] - psum[i] + (K - len) / v.size() * max(0LL, psum[v.size()]));
      |                               ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303: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:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:30:34: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   30 |                         ret = max(ret, psum[j + 1] - psum[i] + (K - len) / v.size() * max(0LL, psum[v.size()]));
      |                               ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~