Language: C++

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<functional>
using namespace std;
#define ll long long int
#define ld long double
using namespace __gnu_pbds;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,
tree_order_statistics_node_update> in_set;
#define rep(n) for(ll i=0;i<(n);i++)
#define jrep(n) for(ll j=0;j<(n);j++)
#define all(a) (a).begin(), (a).end()
#define dpp(a) cout<<(a).first<<" "<<(a).second<<endl;
#define append push_back
#define mod 1000000007
#define ff first
#define ss second
#define ffast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fast ios::sync_with_stdio(0);
ll pw(ll x,ll y,ll p=mod){ll r=1; x=x%p;while(y>0){ if(y&1)r=(r*x)%p;
y=y>>1;x=(x*x)%p;} return r; } vector<ll> uniq(vector<ll> arr)
{vector<ll>brr;sort(all(arr));brr.append(arr[0]);
for(auto i:arr)if(brr.back()!=i)brr.append(i);return brr;}
 
bool visit[5005];
void solve(){
    ll n,k; cin>>n>>k;
    vector<ll> p(n),c(n); rep(n) cin>>p[i]; rep(n) cin>>c[i];
    rep(n) p[i]--;
    vector<vector<ll>> arr;
//    for(auto i:p) cout<<i<<" "; cout<<endl;
    rep(n) if(!visit[i]){
        visit[i]=true; vector<ll> rw(1,i); ll ti=p[i];
        while(ti!=i) {
            visit[ti]=true;
//            cout<<ti<<" "<<endl;
            rw.append(ti); ti=p[ti];
        }
        arr.append(rw);
    }
    rep(arr.size()) jrep(arr[i].size()) arr[i][j]=c[arr[i][j]];
    ll mx = *max_element(all(c));
    for(auto a:arr){ 
//        for(auto j:a) cout<<j<<" "; cout<<" -- "<<endl;

        ll tn = a.size();
        ll tms = a[0];
        rep(tn){
            ll tk = k;
            ll csu = a[i]; ll in=i; tk--;
            ll su = accumulate(all(a),(ll)0);
            ll ms = su*(tk/tn) + csu;
            jrep(tn-1){
                in++; tk--; in%=tn; csu+=a[in];
                if(tk<0) break;
                ms = max(ms,su*(tk/tn) + csu);
//                cout<<tk<<" "<<su*(tk/tn) + csu<<"  "<<i<<" = "<<ms<<endl;
            }
            tms = max(tms,ms);
        }
        mx = max(mx,tms);
        //cout<<tms<<endl;
//        cout<<" -------------- \n";
    }
    cout<<mx<<endl;
}
 
int main(){
    ffast
    ll t=1;
    //cin>>t;
    rep(t) solve();
}
 
 
// *p.find_by_order(n) : nth value (0 indexed)
// p.order_of_key(7): index of 7 (0 indexed)


Compiler output:
a.cc: In function 'void solve()':
a.cc:33:16: error: reference to 'visit' is ambiguous
   33 |     rep(n) if(!visit[i]){
      |                ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc:1:
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:26:6: note:                 'bool visit [5005]'
   26 | bool visit[5005];
      |      ^~~~~
a.cc:34:9: error: reference to 'visit' is ambiguous
   34 |         visit[i]=true; vector<ll> rw(1,i); ll ti=p[i];
      |         ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:26:6: note:                 'bool visit [5005]'
   26 | bool visit[5005];
      |      ^~~~~
a.cc:36:13: error: reference to 'visit' is ambiguous
   36 |             visit[ti]=true;
      |             ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:26:6: note:                 'bool visit [5005]'
   26 | bool visit[5005];
      |      ^~~~~