Language: C++

#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <limits>
#include <random>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <tuple>
using namespace std;
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
typedef long long ll;
template <class T> using reversed_priority_queue = priority_queue<T, vector<T>, greater<T> >;

ll sum[5010][5010];
bool used[5010];
ll size[5010];

int main ()
{
    ll N,K;
    cin >> N >> K;
    vector<ll> P(N),C(N);
    REP(i,N) cin >> P[i];
    REP(i,N) cin >> C[i];
    REP(i,N) P[i]--;

    REP(i,N){
        ll sumt = 0;
        ll idx = i;
        fill(used, used + 5010, false);
        REP(j,N) {
            idx = P[idx];
            if (used[idx]) {
                size[i] = j;
                break;
            }
            used[idx] = true;
            sumt += C[idx];
            sum[i][j + 1] = sumt;
            if (j == N - 1) {
                size[i] = N;
            }
        }
    }

    ll ans = -3e18;
    REP(i,N) {
        ll anst = 0;
        ll Kt = K;
        if (Kt > size[i]) {
            ll t = Kt / size[i];
            Kt -= t * size[i];
            if (sum[i][size[i]] > 0) {
                anst += t * sum[i][size[i]];
                ans = max(ans, anst);
            }
            REP(j,size[i]) {
                ans = max(ans, sum[i][j + 1]);
            }
        }
        REP(j,Kt) {
            ll anstt = anst + sum[i][j + 1];
            if (ans < anstt) {
                ans = anstt;
            }
        }
    }

    cout << ans << endl;

    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:46:17: error: reference to 'size' is ambiguous
   46 |                 size[i] = j;
      |                 ^~~~
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:28:4: note:                 'll size [5010]'
   28 | ll size[5010];
      |    ^~~~
a.cc:53:17: error: reference to 'size' is ambiguous
   53 |                 size[i] = N;
      |                 ^~~~
/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:28:4: note:                 'll size [5010]'
   28 | ll size[5010];
      |    ^~~~
a.cc:62:18: error: reference to 'size' is ambiguous
   62 |         if (Kt > 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:28:4: note:                 'll size [5010]'
   28 | ll size[5010];
      |    ^~~~
a.cc:63:25: error: reference to 'size' is ambiguous
   63 |             ll t = Kt / 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:28:4: note:                 'll size [5010]'
   28 | ll size[5010];
      |    ^~~~
a.cc:64:23: error: reference to 'size' is ambiguous
   64 |             Kt -= t * 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:28:4: note:                 'll size [5010]'
   28 | ll size[5010];
      |    ^~~~
a.cc:65:24: error: reference to 'size' is ambiguous
   65 |             if (sum[i][size[i]] > 0) {
      |                        ^~~~
/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:28:4: note:                 'll size [5010]'
   28 | ll size[5010];
      |    ^~~~
a.cc:66:36: error: reference to 'size' is ambiguous
   66 |                 anst += t * sum[i][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:28:4: note:                 'll size [5010]'
   28 | ll size[5010];
      |    ^~~~
a.cc:69:19: error: reference to 'size' is ambiguous
   69 |             REP(j,size[i]) {
      |                   ^~~~
a.cc:22:42: note: in definition of macro 'REP'
   22 | #define REP(i,n) for(int (i)=0;(i)<(int)(n);(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:28:4: note:                 'll size [5010]'
   28 | ll size[5010];
      |    ^~~~