Language: C++

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define ALL(v) (v).begin(), (v).end()
#define VVL vector<vector<ll>>
ll INF=1e18+5;

int main(){
    ll N, K;
    cin >> N >> K;
    VL P(N);    VL C(N);
    rep(i,0,N) cin >> P[i];
    rep(i,0,N) cin >> C[i];

    ll ans = -1 * INF;    
    ll tmp = 0;
    rep(i,0,N){
        tmp = max(tmp, C[i]);
        ans = max(ans, C[i]);
    }
    if(tmp == 0){
        cout << ans << endl;
        return 0;
    }

    ans = 0;
    VVL a(N);
    rep(i,0,N){
        VL b(N,0);
        b[i] = 1;
        ll np = P[i] - 1;
      	ll cnt = K;
        a[i].push_back(i);
        while(cnt > 0){
            cnt--;
            a[i].push_back(np);
            np = P[np] - 1;
            if(b[np] == 1) break;
            b[np] = 1;
        }
    }
    rep(i,0,N){
        tmp = 0;
        rep(j,0,min(K,a[i].size())){
            tmp += C[a[i][j]]; 
        }
        if(K > a[i].size() - 1){
            ll kk = a[i].size() - 1;
            K -= kk; 
            ll lp = K/(kk+1);
            ll amari = K%(kk+1);
            tmp += tmp*lp;
            rep(j,0,amari){
                tmp += C[a[i][j]];
            }
        }
        ans = max(ans,tmp);
    }

    cout << ans << endl;
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:47:20: error: no matching function for call to 'min(ll&, std::vector<long long int>::size_type)'
   47 |         rep(j,0,min(K,a[i].size())){
      |                 ~~~^~~~~~~~~~~~~~~
a.cc:4:68: note: in definition of macro 'rep'
    4 | #define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); 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:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:47:20: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'})
   47 |         rep(j,0,min(K,a[i].size())){
      |                 ~~~^~~~~~~~~~~~~~~
a.cc:4:68: note: in definition of macro 'rep'
    4 | #define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
      |                                                                    ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281: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:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:47:20: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   47 |         rep(j,0,min(K,a[i].size())){
      |                 ~~~^~~~~~~~~~~~~~~
a.cc:4:68: note: in definition of macro 'rep'
    4 | #define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
      |                                                                    ^~~