Language: C++

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

#define ll long long

ll N, K, G[5000], C[5000];
vector < ll > costs, vis;

void dfs(ll u){
	vis[u] = true;
	costs.push_back(C[u]);
	if (!vis[G[u]])
		dfs(G[u]);
	else
		return;
}

int main(){
	cin >> N >> K;
	for (ll i = 0; i < N; i ++){
		ll X; cin >> X; X -= 1;
		G[i] = X;
	}
	for (ll i = 0; i < N; i ++)
		cin >> C[i];
	
	long long result = -2e18;


	for (ll i = 0; i < N; i ++){
		vis = vector < ll > (N, 0);
		costs.clear();
		dfs(G[i]);
		// for (auto x : costs) cout << x << " "; cout << "\n";
		// cout << "A\n";

		ll sz = costs.size();
		ll p0 = K / sz, p1 = K % sz;
		long long curr1 = 0;
		for (ll i = 0; i < ll(costs.size()) && i < K; i ++){
			curr1 += costs[i];
			// cout << curr1 << "\n";
			result = max(curr1, result);
		}
		// cout << "B\n";
		// cout << curr1 << "\n";

		if (p0 > 0){
			// cout << "A " << p0 << "\n";
			long long curr2 = accumulate(costs.begin(), costs.end(), 0ll) * (p0);
			// cout << curr2 << "\n";	
			for (ll i = 0; i < ll(costs.size()) && i < p1; i ++){
				curr2 += costs[i];
				result = max(curr2, result);
			}
		}
		if (p0 > 1){
			long long curr3 = accumulate(costs.begin(), costs.end(), 0ll) * (p0 - 1);
			for (ll i = 0; i < ll(costs.size()) && i < K; i ++){
				curr3 += costs[i];
				result = max(curr3, result);
			}
		}
	}

	cout << result << "\n";
	// cout << moves << "\n";
	// for (auto x : costs){
	// 	cout << x << " ";
	// }	cout << "\n";

}


Compiler output:
a.cc: In function 'int main()':
a.cc:4:12: error: expected primary-expression before 'long'
    4 | #define ll long long
      |            ^~~~
a.cc:40:36: note: in expansion of macro 'll'
   40 |                 for (ll i = 0; i < ll(costs.size()) && i < K; i ++){
      |                                    ^~
a.cc:40:35: error: expected ';' before 'long'
   40 |                 for (ll i = 0; i < ll(costs.size()) && i < K; i ++){
      |                                   ^
      |                                   ;
a.cc:4:12: error: expected primary-expression before 'long'
    4 | #define ll long long
      |            ^~~~
a.cc:40:36: note: in expansion of macro 'll'
   40 |                 for (ll i = 0; i < ll(costs.size()) && i < K; i ++){
      |                                    ^~
a.cc:40:35: error: expected ')' before 'long'
   40 |                 for (ll i = 0; i < ll(costs.size()) && i < K; i ++){
      |                     ~             ^
      |                                   )
a.cc:4:12: error: expected primary-expression before 'long'
    4 | #define ll long long
      |            ^~~~
a.cc:40:36: note: in expansion of macro 'll'
   40 |                 for (ll i = 0; i < ll(costs.size()) && i < K; i ++){
      |                                    ^~
a.cc:40:67: error: expected ';' before ')' token
   40 |                 for (ll i = 0; i < ll(costs.size()) && i < K; i ++){
      |                                                                   ^
      |                                                                   ;
a.cc:4:12: error: expected primary-expression before 'long'
    4 | #define ll long long
      |            ^~~~
a.cc:52:44: note: in expansion of macro 'll'
   52 |                         for (ll i = 0; i < ll(costs.size()) && i < p1; i ++){
      |                                            ^~
a.cc:52:43: error: expected ';' before 'long'
   52 |                         for (ll i = 0; i < ll(costs.size()) && i < p1; i ++){
      |                                           ^
      |                                           ;
a.cc:4:12: error: expected primary-expression before 'long'
    4 | #define ll long long
      |            ^~~~
a.cc:52:44: note: in expansion of macro 'll'
   52 |                         for (ll i = 0; i < ll(costs.size()) && i < p1; i ++){
      |                                            ^~
a.cc:52:43: error: expected ')' before 'long'
   52 |                         for (ll i = 0; i < ll(costs.size()) && i < p1; i ++){
      |                             ~             ^
      |                                           )
a.cc:4:12: error: expected primary-expression before 'long'
    4 | #define ll long long
      |            ^~~~
a.cc:52:44: note: in expansion of macro 'll'
   52 |                         for (ll i = 0; i < ll(costs.size()) && i < p1; i ++){
      |                                            ^~
a.cc:52:76: error: expected ';' before ')' token
   52 |                         for (ll i = 0; i < ll(costs.size()) && i < p1; i ++){
      |                                                                            ^
      |                                                                            ;
a.cc:4:12: error: expected primary-expression before 'long'
    4 | #define ll long long
      |            ^~~~
a.cc:59:44: note: in expansion of macro 'll'
   59 |                         for (ll i = 0; i < ll(costs.size()) && i < K; i ++){
      |                                            ^~
a.cc:59:43: error: expected ';' before 'long'
   59 |                         for (ll i = 0; i < ll(costs.size()) && i < K; i ++){
      |                                           ^
      |                                           ;
a.cc:4:12: error: expected primary-expression before 'long'
    4 | #define ll long long
      |            ^~~~
a.cc:59:44: note: in expansion of macro 'll'
   59 |                         for (ll i = 0; i < ll(costs.size()) && i < K; i ++){
      |                                            ^~
a.cc:59:43: error: expected ')' before 'long'
   59 |                         for (ll i = 0; i < ll(costs.size()) && i < K; i ++){
      |                             ~             ^
      |                                           )
a.cc:4:12: error: expected primary-expression before 'long'
    4 | #define ll long long
      |            ^~~~
a.cc:59:44: note: in expansion of macro 'll'
   59 |                         for (ll i = 0; i < ll(costs.size()) && i < K; i ++){
      |                                            ^~
a.cc:59:75: error: expected ';' before ')' token
   59 |                         for (ll i = 0; i < ll(costs.size()) && i < K; i ++){
      |                                                                           ^
      |                                                                           ;