Language: C++

#include <bits/stdc++.h>
#define endl '\n'
#define LL long long
#define LD long double
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define debug(x) cerr << #x << " is " << x << endl;
using namespace std;
int const MAXN = 5000 + 9;
LL c[MAXN], p[MAXN], cost[MAXN];
int vis[MAXN], root[MAXN];
LL size[MAXN];
int find_set(int x){
    if(root[x] == x) return x;
    return root[x] = find_set(root[x]);
}
void dfs(int node){
    vis[node] = 1;
    if(!vis[p[node]]){
        dfs(p[node]);
        int u = find_set(node);
        int v = find_set(p[node]);
        if(u > v) swap(u,v);
        size[u] += size[v];
        cost[u] += cost[v];
        root[v] = u;
    }
}
LL go(int node, int s){
    if(s == 0) return 0;
    return c[p[node]] + max(go(p[node], s - 1),0LL);
}
int main(){
    ios_base::sync_with_stdio (0),cin.tie(0);
    int n, k;
    cin >> n >> k;
    for(int i = 1; i <= n; i++){
        cin >> p[i];
        size[i] = 1;
        root[i] = i;
        
    }
    LL ans = -1e18;
    for(int i = 1; i <= n; i++){
        cin >> c[i];
        cost[i] = c[i];
        ans = max(c[i], ans);
    }
    for(int i = 1; i <= n; i++){
        if(!vis[i]){
            dfs(i);
        }
    }
    for(int i = 1; i <= n; i++){
        int u = find_set(i);
        LL cur = (k / size[u]) * cost[u];
        if(cost[u] > 0){
            ans = max(cur,ans);
            cur += go(i, k % size[u]);
        }else {
            cur = 0;
            cur += go(i, size[u]);
        }
        ans = max(ans,cur);
    }
    cout << ans << endl;

}

Compiler output:
a.cc: In function 'void dfs(int)':
a.cc:26:9: error: reference to 'size' is ambiguous
   26 |         size[u] += size[v];
      |         ^~~~
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:14:4: note:                 'long long int size [5009]'
   14 | LL size[MAXN];
      |    ^~~~
a.cc:26:20: error: reference to 'size' is ambiguous
   26 |         size[u] += 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:14:4: note:                 'long long int size [5009]'
   14 | LL size[MAXN];
      |    ^~~~
a.cc: In function 'int main()':
a.cc:41:9: error: reference to 'size' is ambiguous
   41 |         size[i] = 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:14:4: note:                 'long long int size [5009]'
   14 | LL size[MAXN];
      |    ^~~~
a.cc:58:23: error: reference to 'size' is ambiguous
   58 |         LL cur = (k / size[u]) * cost[u];
      |                       ^~~~
/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:14:4: note:                 'long long int size [5009]'
   14 | LL size[MAXN];
      |    ^~~~
a.cc:61:30: error: reference to 'size' is ambiguous
   61 |             cur += go(i, k % size[u]);
      |                              ^~~~
/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:14:4: note:                 'long long int size [5009]'
   14 | LL size[MAXN];
      |    ^~~~
a.cc:64:26: error: reference to 'size' is ambiguous
   64 |             cur += go(i, size[u]);
      |                          ^~~~
/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:14:4: note:                 'long long int size [5009]'
   14 | LL size[MAXN];
      |    ^~~~