Language: C++

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

#define INF 2000000000

long long int dp[5050][5050],dp_max[5050][5050];
int size[5050];
int p[5050];
long long int c[5050];


int main(){

    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 = 0;i <= N;i++){
        for(int j = 0;j <= N;j++){
            dp_max[i][j] = -100000000000;
        }
    }

    for(int i = 1;i <= N;i++){

        int now = i;
        int count = 1;
        while(true){
            dp[i][count] = dp[i][count-1] + c[p[now]];
            dp_max[i][count] = max(dp_max[i][count-1],dp[i][count]);
            now = p[now];
            if(now == i){
                size[i] = count;
                break;
            }
            count++;
        }

    }
    long long int ans = -100000000000;
    for(int i = 1;i <= N;i++){
        if(K <= size[i]){
            ans = max(ans,dp_max[i][K]);
        }else{
            if(dp[i][size[i]] < 0){
                ans = max(ans,dp_max[i][K]);
            }else{
                long long int tmp_ans;
                tmp_ans = dp[i][size[i]] * (K / size[i]) + dp_max[i][K % size[i]];
                ans = max(ans,tmp_ans);
            }
        }
    }

    cout << ans << endl;

/*
    for(int i = 1;i <= N;i++){
        for(int j = 1;j <= size[i];j++){
            cout << dp[i][j] << " ";
        }
        cout << endl;
    }

    for(int i = 1;i <= N;i++){
        for(int j = 1;j <= size[i];j++){
            cout << dp_max[i][j] << " ";
        }
        cout << endl;
    }

*/
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:40:17: error: reference to 'size' is ambiguous
   40 |                 size[i] = count;
      |                 ^~~~
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:7:5: note:                 'int size [5050]'
    7 | int size[5050];
      |     ^~~~
a.cc:49:17: error: reference to 'size' is ambiguous
   49 |         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:7:5: note:                 'int size [5050]'
    7 | int size[5050];
      |     ^~~~
a.cc:52:22: error: reference to 'size' is ambiguous
   52 |             if(dp[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:7:5: note:                 'int size [5050]'
    7 | int size[5050];
      |     ^~~~
a.cc:56:33: error: reference to 'size' is ambiguous
   56 |                 tmp_ans = dp[i][size[i]] * (K / size[i]) + dp_max[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:7:5: note:                 'int size [5050]'
    7 | int size[5050];
      |     ^~~~
a.cc:56:49: error: reference to 'size' is ambiguous
   56 |                 tmp_ans = dp[i][size[i]] * (K / size[i]) + dp_max[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:7:5: note:                 'int size [5050]'
    7 | int size[5050];
      |     ^~~~
a.cc:56:74: error: reference to 'size' is ambiguous
   56 |                 tmp_ans = dp[i][size[i]] * (K / size[i]) + dp_max[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:7:5: note:                 'int size [5050]'
    7 | int size[5050];
      |     ^~~~