Language: C++

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

// #define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

#define FI ios_base::sync_with_stdio(0); cin.tie(NULL);

#define LL long long

const int MOD = 1e9 + 7;
const int INF = 1e9 + 9;
const int MX = 1e5 + 5;

#define int LL;

int n,k;
int per[5005];
int inp[5005];

vector<int> getCycle(int i) {
	vector<int> ret;
	int start = i;

	ret.push_back(start);
	int curr = per[i];

	while(curr != start) {
		ret.push_back(curr);
		curr = per[curr];
	}
	return ret;
}

int main() {
	cin >> n >> k;
	for(int i=1;i<=n;i++) {
		cin >> per[i];
	}
	LL maxV = -INF;
	for(int i=1;i<=n;i++) {
		cin >> inp[i];
		maxV = max(maxV, 1ll*inp[i]);
	}
	LL ans = maxV;

	for(int i=1;i<=n;i++) {
		vector<int> cy = getCycle(i);

		// only 1 move
		// ans = max(ans, cy[1]);

		int cySz = cy.size();
		LL totSum = 0;
		for(int j=0;j<cy.size();j++) {
			totSum += 1ll * inp[cy[j]];
		}

		bool madeMove = false;

		LL calc = 0;
		int rem = k;
		if(totSum > 0) {
			int numCy = k/cySz;
			calc = 1ll * numCy * totSum;
			rem = k%cySz;

			if(numCy > 0) {
				//move made
				madeMove = true;
				ans = max(ans, calc);
			}
		} 

		LL maxVal = -INF;
		LL preSum = 0;
		for(int j=1;j<cy.size() && rem > 0;j++) {
			//make move
			preSum += 1ll * inp[cy[j]];
			rem--;
			maxVal = max(maxVal, preSum);
		}

		trace(maxVal);

		if(madeMove) {
			if(maxVal > 0) {
				ans = max(ans, calc + 1ll * maxVal);
			}
		}
		else {
			ans = max(ans, maxVal);
			if(rem > 0) {
				preSum += 1ll * inp[cy[0]];
				ans = max(ans, preSum);
			}
		}
	}
	cout << ans << endl;

	return 0;
}


Compiler output:
a.cc:21:17: error: declaration does not declare anything [-fpermissive]
   21 | #define LL long long
      |                 ^~~~
a.cc:27:13: note: in expansion of macro 'LL'
   27 | #define int LL;
      |             ^~
a.cc:29:1: note: in expansion of macro 'int'
   29 | int n,k;
      | ^~~
a.cc:29:5: error: 'n' does not name a type
   29 | int n,k;
      |     ^
a.cc:21:17: error: declaration does not declare anything [-fpermissive]
   21 | #define LL long long
      |                 ^~~~
a.cc:27:13: note: in expansion of macro 'LL'
   27 | #define int LL;
      |             ^~
a.cc:30:1: note: in expansion of macro 'int'
   30 | int per[5005];
      | ^~~
a.cc:30:5: error: 'per' does not name a type
   30 | int per[5005];
      |     ^~~
a.cc:21:17: error: declaration does not declare anything [-fpermissive]
   21 | #define LL long long
      |                 ^~~~
a.cc:27:13: note: in expansion of macro 'LL'
   27 | #define int LL;
      |             ^~
a.cc:31:1: note: in expansion of macro 'int'
   31 | int inp[5005];
      | ^~~
a.cc:31:5: error: 'inp' does not name a type; did you mean 'int'?
   31 | int inp[5005];
      |     ^~~
      |     int
a.cc:21:17: error: template argument 1 is invalid
   21 | #define LL long long
      |                 ^~~~
a.cc:27:13: note: in expansion of macro 'LL'
   27 | #define int LL;
      |             ^~
a.cc:33:8: note: in expansion of macro 'int'
   33 | vector<int> getCycle(int i) {
      |        ^~~
a.cc:21:17: error: template argument 2 is invalid
   21 | #define LL long long
      |                 ^~~~
a.cc:27:13: note: in expansion of macro 'LL'
   27 | #define int LL;
      |             ^~
a.cc:33:8: note: in expansion of macro 'int'
   33 | vector<int> getCycle(int i) {
      |        ^~~
a.cc:33:11: error: expected unqualified-id before '>' token
   33 | vector<int> getCycle(int i) {
      |           ^
a.cc:33:26: error: 'i' does not name a type
   33 | vector<int> getCycle(int i) {
      |                          ^
a.cc:21:17: error: declaration does not declare anything [-fpermissive]
   21 | #define LL long long
      |                 ^~~~
a.cc:27:13: note: in expansion of macro 'LL'
   27 | #define int LL;
      |             ^~
a.cc:47:1: note: in expansion of macro 'int'
   47 | int main() {
      | ^~~
a.cc:47:5: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   47 | int main() {
      |     ^~~~
a.cc: In function 'int main()':
a.cc:48:16: error: 'n' was not declared in this scope; did you mean 'yn'?
   48 |         cin >> n >> k;
      |                ^
      |                yn
a.cc:48:21: error: 'k' was not declared in this scope
   48 |         cin >> n >> k;
      |                     ^
a.cc:21:17: error: declaration does not declare anything [-fpermissive]
   21 | #define LL long long
      |                 ^~~~
a.cc:27:13: note: in expansion of macro 'LL'
   27 | #define int LL;
      |             ^~
a.cc:49:13: note: in expansion of macro 'int'
   49 |         for(int i=1;i<=n;i++) {
      |             ^~~
a.cc:49:17: error: 'i' was not declared in this scope
   49 |         for(int i=1;i<=n;i++) {
      |                 ^
a.cc:49:25: error: expected ')' before ';' token
   49 |         for(int i=1;i<=n;i++) {
      |            ~            ^
      |                         )
a.cc:49:26: error: 'i' was not declared in this scope
   49 |         for(int i=1;i<=n;i++) {
      |                          ^
a.cc:21:17: error: declaration does not declare anything [-fpermissive]
   21 | #define LL long long
      |                 ^~~~
a.cc:27:13: note: in expansion of macro 'LL'
   27 | #define int LL;
      |             ^~
a.cc:53:13: note: in expansion of macro 'int'
   53 |         for(int i=1;i<=n;i++) {
      |             ^~~
a.cc:53:25: error: expected ')' before ';' token
   53 |         for(int i=1;i<=n;i++) {
      |            ~            ^
      |                         )
a.cc:21:17: error: declaration does not declare anything [-fpermissive]
   21 | #define LL long long
      |                 ^~~~
a.cc:27:13: note: in expansion of macro 'LL'
   27 | #define int LL;
      |             ^~
a.cc:59:13: note: in expansion of macro 'int'
   59 |         for(int i=1;i<=n;i++) {
      |             ^~~
a.cc:59:25: error: expected ')' before ';' token
   59 |         for(int i=1;i<=n;i++) {
      |            ~            ^
      |                         )