Language: C++

#pragma region
#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <list>
#include <iomanip>
#include <cstdint>
#include <bitset>

using namespace std;
typedef long long ll;
//#define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s) - 1; (i) >= 0; --(i))
#define all(x) x.begin(),x.end()

#pragma region UnionFind
struct UnionFind
{
	vector<int> par;

	UnionFind(int n) : par(n, -1) {}
	void init(int n) { par.assign(n, -1); }

	int root(int x)
	{
		if (par[x] < 0) return x;
		else return par[x] = root(par[x]);
	}

	bool issame(int x, int y)
	{
		return root(x) == root(y);
	}

	bool merge(int x, int y)
	{
		x = root(x); y = root(y);
		if (x == y) return false;
		if (par[x] > par[y]) swap(x, y);
		par[x] += par[y];
		par[y] = x;
		return true;
	}

	int size(int x)
	{
		return -par[root(x)];
	}
};
#pragma endregion
#pragma region GCD
ll gcd(ll a, ll b)
{
	if (b == 0)return a;
	return gcd(b, a%b);
}
#pragma endregion
#pragma region LCM
ll lcm(ll a, ll b)
{
	return a / gcd(a, b) * b;
}
#pragma endregion
#pragma region chmin
template<class T> inline bool chmin(T& a, T b)
{
	if (a > b)
	{
		a = b;
		return true;
	}
	return false;
}
#pragma endregion
#pragma region chmax
template<class T> inline bool chmax(T& a, T b)
{
	if (a < b)
	{
		a = b;
		return true;
	}
	return false;
}
#pragma endregion
#pragma endregion


int main()
{
	ll n, k; cin >> n >> k;
	vector<int> p(n + 1); rep(i, n)cin >> p[i + 1];
	vector<ll> c(n + 1); rep(i, n)cin >> c[i + 1];
	ll res = -1e18;
	if (k <= 1e4)
	{
		for (int i = 1; i <= n; ++i)
		{
			ll tmp = 0;
			ll mx = -1e18;
			int now = i;
			int next = -1;
			rep(j, k)
			{
				next = p[now];
				tmp += c[next];
				chmax(mx, tmp);
				now = next;
			}
			chmax(res, mx);
		}
		cout << res << endl;
	}
	else
	{
		for (int i = 1; i <= n; ++i)
		{
			ll tmp = 0;
			ll mx = -1e18;
			int now = i;
			rep(j, min(k, 15000))
			{
				int next = p[now];
				tmp += c[next];
				chmax(mx, tmp);
				now = next;
			}
			chmax(res, mx);
			vector<int> passed(n + 1, -1);
			now = i;
			int loop_start = -1;
			int loop_step = -1;
			passed[now] = 0;
			rep(j, k)
			{
				int next = p[now];
				if (passed[next] != -1)
				{
					loop_start = next;
					loop_step = passed[now] + 1 - passed[loop_start];
					break;
				}
				passed[next] = passed[now] + 1;
				now = next;
			}
			ll ini_score = 0;
			int ini_step = 0;
			now = i;
			rep(j, k)
			{
				if (now == loop_start)break;
				++ini_step;
				int next = p[now];
				ini_score += c[next];
				now = next;
			}
			ll loop_score = 0;
			now = loop_start;
			rep(j, 10000)
			{
				int next = p[now];
				loop_score += c[next];
				if (next == loop_start)break;
				now = next;
			}
			k -= ini_step;
			tmp = ini_score;
			mx = -1e18;
			tmp += (k / loop_step)*loop_score;
			k %= loop_step;
			now = loop_start;
			chmax(mx, tmp);
			rep(j, k)
			{
				int next = p[now];
				tmp += c[next];
				chmax(mx, tmp);
				now = next;
			}
			chmax(res, mx);
		}
		cout << res << endl;
	}
}

Compiler output:
a.cc: In function 'int main()':
a.cc:130:35: error: no matching function for call to 'min(ll&, int)'
  130 |                         rep(j, min(k, 15000))
      |                                ~~~^~~~~~~~~~
a.cc:21:43: note: in definition of macro 'rep'
   21 | #define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
      |                                           ^
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:3:
/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:130:35: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
  130 |                         rep(j, min(k, 15000))
      |                                ~~~^~~~~~~~~~
a.cc:21:43: note: in definition of macro 'rep'
   21 | #define rep(i, e) for (int(i) = 0; (i) < (e); ++(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,
                 from a.cc:5:
/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:130:35: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  130 |                         rep(j, min(k, 15000))
      |                                ~~~^~~~~~~~~~
a.cc:21:43: note: in definition of macro 'rep'
   21 | #define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
      |                                           ^