Language: C++

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

typedef long long ll;

int n, k;
ll p[5050], c[5050];
int fst[5050], nxt[5050], tot = 0;
int size[5050];
bool used[5050];

struct qer
{
    int f, t;
    ll d;
} es[5050];

void build(int f, int t, ll d)
{
    es[++tot] = (qer){f, t, d};
    nxt[tot] = fst[f];
    fst[f] = tot;
}

ll ans = -1e18;

void dfs(int x, int fa, int ls)
{
    used[x] = 1;
    if (x == fa && ls != 0)
    {
        size[x] = ls;
        return;
    }
    for (int i = fst[x]; i; i = nxt[i])
    {
        int v = es[i].t;
        dfs(v, fa, ls + 1);
        size[x] = size[v];
    }
}

void dfs1(int x, int kk, int c, ll d)
{
    if (c != 0)
        ans = max(d, ans);
    if (c == kk)
    {
        return;
    }
    for (int i = fst[x]; i; i = nxt[i])
    {
        int v = es[i].t;
        if (c == 0)
            dfs1(v, kk, c + 1, es[i].d);
        else
            dfs1(v, kk, c + 1, d + es[i].d);
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> p[i];
    for (int i = 1; i <= n; i++)
        cin >> c[i];
    for (int i = 1; i <= n; i++)
        build(i, p[i], c[p[i]]);
    for (int i = 1; i <= n; i++)
    {
        if (!used[i])
        {
            used[i] = 1;
            dfs(i, i, 0);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        int ls = k;
        if (k > size[i])
        {
            int ls = k % size[i];
            if (ls == 0)
                ls = size[i];
        }
        dfs1(i, ls, 0, 0);
    }
    cout << ans << endl;
    return 0;
}

Compiler output:
a.cc: In function 'void dfs(int, int, int)':
a.cc:32:9: error: reference to 'size' is ambiguous
   32 |         size[x] = ls;
      |         ^~~~
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:9:5: note:                 'int size [5050]'
    9 | int size[5050];
      |     ^~~~
a.cc:39:9: error: reference to 'size' is ambiguous
   39 |         size[x] = size[v];
      |         ^~~~
/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:9:5: note:                 'int size [5050]'
    9 | int size[5050];
      |     ^~~~
a.cc:39:19: error: reference to 'size' is ambiguous
   39 |         size[x] = size[v];
      |                   ^~~~
/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:9:5: note:                 'int size [5050]'
    9 | int size[5050];
      |     ^~~~
a.cc: In function 'int main()':
a.cc:82:17: error: reference to 'size' is ambiguous
   82 |         if (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:9:5: note:                 'int size [5050]'
    9 | int size[5050];
      |     ^~~~
a.cc:84:26: error: reference to 'size' is ambiguous
   84 |             int ls = 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:9:5: note:                 'int size [5050]'
    9 | int size[5050];
      |     ^~~~
a.cc:86:22: error: reference to 'size' is ambiguous
   86 |                 ls = 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:9:5: note:                 'int size [5050]'
    9 | int size[5050];
      |     ^~~~