Language: C++

#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.141592653589793;
const long double DEL = 1e-10;
const int mod =  1000000007;

#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define mpq priority_queue<int,vector<int>,greater<int>>
#define deb(a) cerr << #a << ": " << a << endl
#define vvpii vector<vector<pair<int,int>>>
#define ftt cin>>t;for(int tt=1;tt<=t;++tt)
#define Rev(v) reverse(v.begin(),v.end())
#define Sort(v) sort(v.begin(),v.end())
#define mem(a,b) memset(a,b,sizeof(a))
#define forr(i,a,b) for(i=a;i<=b;i++)
#define umii unordered_map<int,int>
#define vpii vector<pair<int,int>>
#define all(a) a.begin() , a.end()
#define vvi vector<vector<int>>
#define pq priority_queue<int>
#define sqr(a) (((a) * (a)))
#define double long double
#define dbg cout<<"\nhi\n"
#define pii pair<int,int>
#define mii map<int,int>
#define vb vector<bool>
#define vi vector<int>
#define int long long
#define nl cout<<"\n"
#define pb push_back
#define sp <<" "<<
#define ss second
#define ff first
inline void read(int &x)
{
      int c = getchar();
      x = 0;
      int neg = 0;
      for(; ((c<48 || c>57) && c != '-'); c = getchar());
      if(c=='-')
      {
            neg = 1;
            c = getchar();
      }
      for(; c>47 && c<58 ; c = getchar())
      {
            x = (x<<1) + (x<<3) + c - 48;
      }
      if(neg)
      {
            x = -x;
      }
}
int fpow(int x, int n)
{
    int res = 1;
    while(n){
          if(n&1){
                res = res * x % mod;
          }
          x = x * x % mod;
          n>>=1;
    }
    return res;
}
int gcd(int a,int b)
{
    if(b == 0)
    {
          return a;
    }
    return gcd(b,a % b);
}
void sieve(int n)
{
      bool prime[1000006];
      memset(prime, true, sizeof(prime));
      int rootn = (int)sqrt(n);
      for (int p = 2; p <= rootn; p++)
      {
            if (prime[p] == true)
            {
                  for (int i = p*p; i <= n; i += p)
                  {
                        prime[i] = false;
                  }
            }
      }
      prime[1] = 0;
}
const int N = 5050;
int cnt, mid, mx, mn, ans, a[N], b[N];
int n, m, d, i, j, k, l, p[N], q, r, t, w, x, y, z;
bool f, f1, f2;
string s;
// you can also use __int128 :)

//******************************************* CHECK CONSTRAINTS **************************************************
int size[N],sum[N],vis[N];

void dfs(int i,int lev,int paisa)
{
      if(vis[i] == 1)
      {
            if(i == x)
            {
                  size[i] = lev;
                  sum[i] = paisa;
            }
            return;
      }
      vis[i] = 1;
      dfs(p[i],lev+1,paisa+a[i]);
      vis[i] = 0;
}

void dfs2(int i,int lev,int paisa,int k)
{
      if(vis[i] == 1)
      {
            paisa += (k/size[i])*sum[i];
            k%=size[i];
      }
      if(k == 0)
      {
            ans = max(ans,paisa);
            return;
      }
      ans = max(ans,paisa+a[i]);
      vis[i] = 1;
      dfs2(p[i],lev+1,paisa+a[i],k-1);
      vis[i] = 0;
}

int32_t main()
{     fio;
      cin>>n>>k;
      forr(i,1,n)
      {
            cin>>p[i];
      }
      forr(i,1,n)
      {
            cin>>a[i];
      }
      forr(x,1,n)
      {
            dfs(x,1,0);
      }
      ans = -1e18;
      forr(x,1,n)
      {
            dfs2(x,1,0,k);
      }
      cout<<ans;
}


Compiler output:
a.cc: In function 'void dfs(long long int, long long int, long long int)':
a.cc:107:19: error: reference to 'size' is ambiguous
  107 |                   size[i] = lev;
      |                   ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 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:99:5: note:                 'long long int size [5050]'
   99 | int size[N],sum[N],vis[N];
      |     ^~~~
a.cc: In function 'void dfs2(long long int, long long int, long long int, long long int)':
a.cc:121:25: error: reference to 'size' is ambiguous
  121 |             paisa += (k/size[i])*sum[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:99:5: note:                 'long long int size [5050]'
   99 | int size[N],sum[N],vis[N];
      |     ^~~~
a.cc:122:16: error: reference to 'size' is ambiguous
  122 |             k%=size[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:99:5: note:                 'long long int size [5050]'
   99 | int size[N],sum[N],vis[N];
      |     ^~~~