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()
{
	int 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;
			int next = -1;
			rep(j, min(k, 10000))
			{
				next = p[now];
				tmp += c[next];
				chmax(mx, tmp);
				now = next;
			}
			chmax(res, mx);
			vector<int> passed(n + 1, -1);
			now = i;
			next = -1;
			int loop_start = -1;
			int loop_step = -1;
			passed[now] = 0;
			rep(j, k)
			{
				next = p[now];
				if (passed[next] != -1)
				{
					loop_start = next;
					loop_step = passed[now] + 1 - passed[loop_start];
					break;
				}
				now = next;
			}
			ll ini_score = 0;
			int ini_step = 0;
			now = i;
			next = -1;
			rep(j, k)
			{
				if (now == loop_start)break;
				++ini_step;
				next = p[now];
				ini_score += c[next];
				now = next;
			}
			ll loop_score = 0;
			now = loop_start;
			rep(j, 10000)
			{
				next = p[now];
				loop_score += c[next];
				if (next == loop_start)break;
				now = next;
			}
			k -= ini_step;
			ll tmp = ini_score;
			ll mx = -1e18;
			tmp += (k / loop_step)*loop_score;
			k %= loop_step;
			now = loop_start;
			chmax(mx, tmp);
			rep(j, k)
			{
				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:178:28: error: redeclaration of 'll tmp'
  178 |                         ll tmp = ini_score;
      |                            ^~~
a.cc:127:28: note: 'll tmp' previously declared here
  127 |                         ll tmp = 0;
      |                            ^~~
a.cc:179:28: error: redeclaration of 'll mx'
  179 |                         ll mx = -1e18;
      |                            ^~
a.cc:128:28: note: 'll mx' previously declared here
  128 |                         ll mx = -1e18;
      |                            ^~