Language: C++

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

#define MP make_pair
#define EACH(i,c) for(auto i: c)
#define SORT(c) sort((c).begin(),(c).end())

#define ALL(a)  (a).begin(),(a).end()

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	int N, K;
	cin >> N >> K;
	vector<int> P(N), C(N);
	REP(i, N){
		cin >> P[i];
		P[i]--;
	}
	REP(i, N){
		cin >> C[i];
	}

	vector<int> done(N, 0);

	LL ret = -1e18;

	REP(i, N){
		if(done[i]) continue;
		vector<int> loop;
		int cur = i;
		LL total = 0;
		while(!done[cur]){
			done[cur] = 1;
			cur = P[cur];
			total += C[cur];
			loop.push_back(cur);
		}
		// EACH(l, loop) cerr << l << " ";
		// cerr << endl;
		int n = max(0, K / loop.size() - 1);
		int m = K - loop.size() * n;
		LL score = total * n;
		if(score <= 0){
			score = 0;
			m = min(K, (int)loop.size());
		}
		if(0 < m){
			LL sub_max = -1e18;
			REP(j, loop.size())
			{
				LL sub = 0;
				REP(k, m){
					sub += C[loop[(j + k) % loop.size()]];
					sub_max = max(sub_max, sub);
				}
			}
			score += sub_max;
		}
		ret = max(ret, score);
	}

	cout << ret << endl;

	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:77:28: error: no matching function for call to 'max(int, std::vector<int>::size_type)'
   77 |                 int n = max(0, K / loop.size() - 1);
      |                         ~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/vector:62,
                 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:77:28: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'std::vector<int>::size_type' {aka 'long unsigned int'})
   77 |                 int n = max(0, K / loop.size() - 1);
      |                         ~~~^~~~~~~~~~~~~~~~~~~~~~~~
/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,
                 from a.cc:9:
/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:77:28: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   77 |                 int n = max(0, K / loop.size() - 1);
      |                         ~~~^~~~~~~~~~~~~~~~~~~~~~~~