Language: C++

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <stack>
#include <set>
#include <deque>
#pragma GCC optimize(3 , "Ofast" , "inline")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std ;
#define ios ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define x first
#define y second
#define pb push_back
#define ls rt << 1
#define rs rt << 1 | 1
typedef long long ll ;
const double esp = 1e-6 , pi = acos(-1) ;
typedef pair<int , int> PII ;
const int N = 1e6 + 10 , INF = 0x3f3f3f3f , mod = 1e9 + 7;
ll c[N] , b[N] ;
int e[N] ,idx , ne[N] , h[N] , p[N] ;
ll w[N] ;
void add(int a,  int b , ll c){
  e[idx] = b , ne[idx] = h[a] , w[idx] = c , h[a] = idx ++ ;
}
vector<int> v[N] ;
int vis[N] , size[N] ;
void dfs(int u , int cnt){
  vis[u] = cnt ;
  size[cnt] ++ ;
  v[cnt].push_back(u) ;
  for(int i = h[u] ; ~i ; i = ne[i]) {
    int v = e[i] ;
    b[cnt] += w[i] ;
    if(vis[v]) continue ;
    // b[cnt] += w[i] ;
    dfs(v , cnt) ;
  }
}
ll bfs2(int u , int k){
  ll ans = 0 ;
  if(k == 0) return 0 ;
  for(int i = h[u] ; ~i ;i = ne[i]) {
    ans += c[u] ;
    return ans + bfs2(e[i] , k - 1) ;
  }
}
ll bfs1(int u , ll k){
  ll ans = -1e18 ;
  for(auto x : v[u]) {

    for(int i = h[x] ; ~i ; i = ne[i])
      ans = max(ans , bfs2(e[i] , k)) ;
  }
  return ans ;
}
int work()
{
  memset(h , -1 , sizeof h) ;
  int n , k ;
  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 ++ ) {
    add(i , p[i] , c[p[i]]) ;
  }
  int cnt = 0 ;
  for(int i = 1; i <= n ;i ++ )
   if(!vis[i]) cnt ++ , dfs(i , cnt) ;

  ll ans = -1e18 ;
  for(int i = 1; i <= cnt ;i ++ ) {
    if(b[i] < 0) {
      ans = max(ans , bfs1(i , min(k , size[i]))) ;
    }else {
      ans = max(ans , 1ll * b[i] * (k / size[i]) + bfs1(i, k % size[i])) ;
    }
  }
  cout << ans << endl ;
  return 0 ;
}
int main()
{
  //   freopen("C:UsersspnooyseedDesktopin.txt" , "r" , stdin) ;
  //   freopen("C:UsersspnooyseedDesktopout.txt" , "w" , stdout) ;

  work() ;
  return 0 ;
}
/*
*/


Compiler output:
a.cc: In function 'void dfs(int, int)':
a.cc:41:3: error: reference to 'size' is ambiguous
   41 |   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:38:14: note:                 'int size [1000010]'
   38 | int vis[N] , size[N] ;
      |              ^~~~
a.cc: In function 'int work()':
a.cc:85:40: error: reference to 'size' is ambiguous
   85 |       ans = max(ans , bfs1(i , min(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:38:14: note:                 'int size [1000010]'
   38 | int vis[N] , size[N] ;
      |              ^~~~
a.cc:87:41: error: reference to 'size' is ambiguous
   87 |       ans = max(ans , 1ll * b[i] * (k / size[i]) + bfs1(i, 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:38:14: note:                 'int size [1000010]'
   38 | int vis[N] , size[N] ;
      |              ^~~~
a.cc:87:64: error: reference to 'size' is ambiguous
   87 |       ans = max(ans , 1ll * b[i] * (k / size[i]) + bfs1(i, 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:38:14: note:                 'int size [1000010]'
   38 | int vis[N] , size[N] ;
      |              ^~~~
a.cc: In function 'll bfs2(int, int)':
a.cc:58:1: warning: control reaches end of non-void function [-Wreturn-type]
   58 | }
      | ^