Language: C++

				ans = max(ans, max(0, aux-sum[find(i)]) + dfs(p[i], j));
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
#include <string>
#include <cstring>
#include <set>
#include <map>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pair<int, int>> piii;
typedef vector<int> vi;
typedef vector<pii> vii;

#define ri(a) scanf("%d", &a)
#define rii(a,b) scanf("%d %d", &a, &b)
#define riii(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define rl(a) scanf("%lld", &a)
#define rll(a,b) scanf("%lld %lld", &a, &b)
#define FOR(i,n,m) for(ll i=n; i<m; i++)
#define ROF(i,n,m) for(int i=n; i>m; i--)
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second

const int INF = 0x3f3f3f3f;
const ll LLINF = 1e18;
const int maxN = 5010; // CAMBIAR ESTE

// GJNM
int n, k;
int p[maxN];
ll c[maxN];

int father[maxN], siz[maxN], n_elem[maxN];
ll sum[maxN];
// n es el primer elemento y m el ultimo, por ejemplo los nodos de un grafo desde el 1 al 10
void makeSet(int n, int m) {
	for (int i = n; i <= m; ++i) {
		father[i] = -1;
		siz[i] = 0;
		sum[i] = c[i];
		n_elem[i] = 1;
	}
}
int find(int x) {
	if (father[x] == -1) // -1 significa que es el representante del set
		return x;
	return father[x] = find(father[x]);
}
void unio(int x, int y) { // x y y tienen que ser los representantes
	if (siz[x] > siz[y]) {
		father[y] = x;
		sum[x] += sum[y];
		n_elem[x] += n_elem[y];
	}
	else {
		father[x] = y;
		if ( siz[x] == siz[y] ) siz[y]++;
		sum[y] += sum[x];
		n_elem[y] += n_elem[x];
	}
}

ll dp[maxN][maxN];
bool vis[maxN][maxN];


ll dfs(int x, int rem) {
	if ( rem <= 0 )
		return 0ll;
	if ( vis[x][rem] )
		return dp[x][rem];
	vis[x][rem] = true;

	dp[x][rem] = c[x] + dfs(p[x], rem - 1);

	return dp[x][rem];
}


int main() {
	rii(n, k);
	FOR(i, 0, n) {
		ri(p[i]);
		p[i]--;
	}
	FOR(i, 0, n) {
		rl(c[i]);
	}
	makeSet(0, n);
	FOR(i, 0, n) {
		if ( find(i) != find(p[i]) ) {
			unio(find(i), find(p[i]));
		}
	}
	ll ans = -50000000000000ll;
	FOR(i, 0, n) {
		ll aux = (k / n_elem[find(i)]) * sum[find(i)];


		if ( k / n_elem[find(i)] > 0 ) {
			FOR(j, 0, (k % n_elem[find(i)]) + 1) {
				ans = max(ans, aux + dfs(p[i], j));
			}
			FOR(j, 0, (k % n_elem[find(i)]) + 1) {
				ans = max(ans, max(0, aux-sum[find(i)]) + dfs(p[i], j));
			}
			FOR(j, 1, n_elem[find(i)] + 1) {
				ans = max(ans, dfs(p[i], j));
			}
		}
		else {
			FOR(j, 1, k + 1) {
				ans = max(ans, dfs(p[i], j));
			}
		}
		//printf("%d %d %lld %lld %lld\n", i + 1, p[i] + 1, c[i], c[p[i]], ans);
	}
	printf("%lld\n", ans);
	return 0;
}

Compiler output:
a.cc:1:33: error: 'ans' does not name a type
    1 |                                 ans = max(ans, max(0, aux-sum[find(i)]) + dfs(p[i], j));
      |                                 ^~~
a.cc: In function 'int main()':
a.cc:116:51: error: no matching function for call to 'max(int, ll)'
  116 |                                 ans = max(ans, max(0, aux-sum[find(i)]) + dfs(p[i], j));
      |                                                ~~~^~~~~~~~~~~~~~~~~~~~~
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:2:
/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:116:51: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'll' {aka 'long long int'})
  116 |                                 ans = max(ans, max(0, aux-sum[find(i)]) + dfs(p[i], j));
      |                                                ~~~^~~~~~~~~~~~~~~~~~~~~
/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:6:
/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:116:51: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  116 |                                 ans = max(ans, max(0, aux-sum[find(i)]) + dfs(p[i], j));
      |                                                ~~~^~~~~~~~~~~~~~~~~~~~~