Language: C++

#include <bits/stdc++.h>
using namespace std;
 
#define lli long long int
#define REP(i,s,n) for(int i=s;i<n;i++)
#define NUM 2520
#define INF (1LL<<50)
#define DEBUG 0
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) # VariableName
#define LOG(x) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<endl;
#define LOG2(x,y) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<endl;
#define LOG3(x,y,z) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
#define LOG4(w,x,y,z) if(DEBUG)cout<<TO_STRING(w)<<"="<<w<<" "<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
 
template<class T>bool chmax(T & a, const T & b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
 
int dp[64][5100];
lli score[64][5100];
int loopCnt[5100];
int P[5100];
int C[5100];
 
void func(int N, int K){

    lli ans = -INF;
    REP(i,0,N){
        dp[0][i] = P[i];
        score[0][i] = C[P[i]];
        chmax(ans,score[0][i]);
    }
 
    REP(i,0,30){
        REP(j,0,N){
            dp[i+1][j] = dp[i][dp[i][j]];
            score[i+1][j] = score[i][dp[i][j]]+score[i][j];
        }
    }

    REP(i,0,N){
        int now = P[i];
        if(now==i){
            loopCnt[i]=2;
            continue;
        }
        int cnt = 1;
        while(now!=i){
            now = P[now];
            cnt++;
        }
        loopCnt[i]=cnt+1;
    }
 
        REP(j,0,N){
            REP(i,max(0LL,K-loopCnt[j]),K+1){
                
                lli sum = 0;
                int now = j;
                REP(k,0,30){
                    if(i&(1LL<<k)){
                        sum += score[k][now];
                        now = dp[k][now];
 
                        chmax(ans,sum);
                    }
                }
            }
        }
    
    cout<<ans<<endl;
 
}
 
int main(){
    // cout << fixed << setprecision(5);
 
    long long N;
    scanf("%lld",&N);
    long long K;
    scanf("%lld",&K);
    for(int i = 0 ; i < N ; i++){
        scanf("%d",&P[i]);
        P[i]--;
    }
    for(int i = 0 ; i < N ; i++){
        scanf("%d",&C[i]);
    }
    func(N, K);
    return 0;
}

Compiler output:
a.cc: In function 'void func(int, int)':
a.cc:58:22: error: no matching function for call to 'max(long long int, int)'
   58 |             REP(i,max(0LL,K-loopCnt[j]),K+1){
      |                   ~~~^~~~~~~~~~~~~~~~~~
a.cc:5:30: note: in definition of macro 'REP'
    5 | #define REP(i,s,n) for(int i=s;i<n;i++)
      |                              ^
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:58:22: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   58 |             REP(i,max(0LL,K-loopCnt[j]),K+1){
      |                   ~~~^~~~~~~~~~~~~~~~~~
a.cc:5:30: note: in definition of macro 'REP'
    5 | #define REP(i,s,n) for(int i=s;i<n;i++)
      |                              ^
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:58:22: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   58 |             REP(i,max(0LL,K-loopCnt[j]),K+1){
      |                   ~~~^~~~~~~~~~~~~~~~~~
a.cc:5:30: note: in definition of macro 'REP'
    5 | #define REP(i,s,n) for(int i=s;i<n;i++)
      |                              ^