Language: C++

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<cstdlib>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long LL;
typedef pair<int,int> PII;
#define X first
#define Y second
inline int read()
{
    int x=0,f=1;char c=getchar();
    while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
    while(isdigit(c)){x=x*10+c-'0';c=getchar();}
    return x*f;
}
const int maxn=5010;
const LL oo=1ll<<60;
int n,K,p[maxn],col[maxn],cnt,size[maxn];
LL c[maxn],ans=-oo,now_ans;
bool vis[maxn];
vector<LL> V;
int main()
{
    n=read();K=read();
    for(int i=1;i<=n;i++)p[i]=read();
    for(int i=1;i<=n;i++)c[i]=read();
    for(int i=1;i<=n;i++)
        if(!vis[i])
        {
            int now=i;
            col[now]=++cnt;
            vis[now]=1;
            size[cnt]++;
            now=p[now];
            while(now!=i)col[now]=cnt,vis[now]=1,size[cnt]++,now=p[now];
        }
    for(int i=1;i<=n;i++)
    {
    	int now=i;
    	LL pre=c[now];
    	V.clear();
    	V.push_back(pre);
    	now=p[now];
    	while(now!=i)pre+=c[now],V.push_back(pre),now=p[now];
    	int sz=V.size();
		now_ans=0;
		if(K>sz)
		{
			if(pre>=0)
	    	{
	    		now_ans=(LL)(K/sz)*pre;
	    		for(int j=0;j<K%V.size();j++)now_ans=max(now_ans,(LL)(K/sz)*pre+V[j]);
	    	}
	    	else
	    	{
	    		now_ans=V[0];
	    		for(int j=1;j<V.size();j++)now_ans=max(now_ans,V[j]);
	    	}
		}
    	else
    	{
    		now_ans=V[0];
	    		for(int j=1;j<V.size();j++)now_ans=max(now_ans,V[j]);
    	}
//    	printf("%d:\n",i);
//    	for(int j=0;j<V.size();j++)printf("%lld ",V[j]);
//    	printf("%lld\n",now_ans);
//		puts("");
		ans=max(ans,now_ans);
    }
    printf("%lld\n",ans);
    return 0;
}
/*
*/

Compiler output:
a.cc: In function 'int main()':
a.cc:37:13: error: reference to 'size' is ambiguous
   37 |             size[cnt]++;
      |             ^~~~
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:1:
/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:22:31: note:                 'int size [5010]'
   22 | int n,K,p[maxn],col[maxn],cnt,size[maxn];
      |                               ^~~~
a.cc:39:50: error: reference to 'size' is ambiguous
   39 |             while(now!=i)col[now]=cnt,vis[now]=1,size[cnt]++,now=p[now];
      |                                                  ^~~~
/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:22:31: note:                 'int size [5010]'
   22 | int n,K,p[maxn],col[maxn],cnt,size[maxn];
      |                               ^~~~