Language: C++

#include<cstdio>
#include<iostream>
using namespace std;
const int N = 5e3 + 10;
const long long MIN = -1e16;
int n,k,a[N],b[N],val[N],size[N];
long long dp[N],positive[N];
int main()
{

	cin>>n>>k;
	for (int i = 1 ;i <= n ; i++)
	{
		scanf("%d",&a[i]);
	}

	for (int i = 1 ;i <= n ;i++)
	{
		scanf("%d",&b[i]);
	}


	int cnt = 1;
	for (int i = 1; i <= n; i++) val[i] = 0;

	for (int i = 1 ;i <= n ;i++)
	{
		if (val[i] != 0) continue;
		

		int cnt_size = 0;
		int prev = i;
		int next = a[prev];
		val[i] = cnt;
		while(next != i)
		{
			val[prev] = cnt;
			prev = next;
			next = a[prev];
			cnt_size++;
		}
		val[prev] = cnt;
		cnt_size++;
		size[cnt] = cnt_size;
		cnt++;
	}
	cnt--;



	//for (int i = 1; i <= n ; i++) cout<<val[i]<<' ';cout<<endl;
	//cout<<"Velicina svakog skupa: ";
	//for (int i = 1; i <= cnt; i++) cout<<size[i]<<' ';cout<<endl;

	for (int i = 1; i <= cnt; i++) positive[i] = MIN;
	for (int i = 1; i <= n ;i++)
	{
		if( positive[ val[i] ] != MIN ) continue;

		long long suma = 0;
		int prev = i;
		int next = a[prev];
		while(next != i)
		{
			suma+=1LL*b[next];
			prev = next;
			next = a[prev];
		}
		suma+=b[next];

		positive[ val [i] ] = suma;
		//if (suma == 0) positive[ val[i] ] = 0;
		//else		   positive[ val[i] ] = suma;
	}

	//cout<<"Suma svakog skupa: ";
	//for (int i = 1; i <= cnt; i++) cout<<positive[i]<<' '; cout<<endl;
	//cout<<endl;

	for (int i = 1; i <= n ; i++) dp[i] = 0;
	
	for (int i = 1; i <= n ;i++)
	{

		long long mx = MIN;
		long long suma = 0;
		int koraci = k;
		int prev = i;
		int next = a[prev];


		while(next != i && koraci)
		{
			suma += b[next];
			mx = max(mx,suma);
			prev = next;
			next = a[prev];
			koraci--;
		}
		if (koraci)
		{
			suma+=b[next];
			koraci--;
			mx = max(mx,suma);
		}

		if (positive[ val[i] ] <= 0) dp[i] = mx;
		else
		{
			if (koraci >= size[ val[i] ])
			{
				suma += suma * (koraci / size[ val [i] ] );
				koraci = koraci % size[ val[i] ];
			}

			 prev = i;
			 next = a[prev];

			while(next != i && koraci)
			{
				suma += b[next];
				mx = max(mx,suma);
				prev = next;
				next = a[prev];
				koraci--;
			}

			dp[i] = mx;
		}
	}
	//cout<<"dp[i] : ";
	//for (int i = 1 ; i <= n ; i++) cout<<dp[i]<<' ';cout<<endl;

	long long ans = MIN;
	for (int i = 1 ;i <= n ; i++) ans = max (ans,dp[i]);
	cout<<ans<<endl;

return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:44:17: error: reference to 'size' is ambiguous
   44 |                 size[cnt] = cnt_size;
      |                 ^~~~
In file included from /usr/include/c++/14/string:53,
                 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/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:26: note:                 'int size [5010]'
    6 | int n,k,a[N],b[N],val[N],size[N];
      |                          ^~~~
a.cc:110:39: error: reference to 'size' is ambiguous
  110 |                         if (koraci >= size[ val[i] ])
      |                                       ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:26: note:                 'int size [5010]'
    6 | int n,k,a[N],b[N],val[N],size[N];
      |                          ^~~~
a.cc:112:58: error: reference to 'size' is ambiguous
  112 |                                 suma += suma * (koraci / size[ val [i] ] );
      |                                                          ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:26: note:                 'int size [5010]'
    6 | int n,k,a[N],b[N],val[N],size[N];
      |                          ^~~~
a.cc:113:51: error: reference to 'size' is ambiguous
  113 |                                 koraci = koraci % size[ val[i] ];
      |                                                   ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:26: note:                 'int size [5010]'
    6 | int n,k,a[N],b[N],val[N],size[N];
      |                          ^~~~