Language: C++

#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define int long long
#define big 998244353
#define ff first
#define se second
#define pb push_back
#define pii pair<int,int>
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define PSET(x,y) fixed<<setprecision(y)<<x
#define mp make_pair
#define pi 3.141592653589
int power(int x,int y){
    int r=1,z=x;
    while(y){
        if(y & 1)r*=z;
        z*=z;y=y>>1;}
    return r;}
int powerm(int x,int y,int p){
    int r=1;
    while(y){
        if(y & 1)r=(r*x)%p;
        y=y>>1;
        x=(x*x)%p;}
    return r%p;}
int modinv(int x,int m){
    return powerm(x,m-2,m);}
int logarithm(int a,int b){
    int x=0;
    while(a>1){
        x++;
        a/=b;}
    return x;}
int p[5005],c[5005],visit[5005];
vector<int> temp;
void dfs(int i){
    visit[i]=1;
    temp.pb(i);
    if(!visit[p[i]])
        dfs(p[i]);
}
int32_t main(){
    fast;
    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];
    vector<vector<int> > comp;
    for(int i=1;i<=n;i++){
        temp.clear();
        if(!visit[i])
            dfs(i);
        if(temp.size())
            comp.pb(temp);
    }
    int fans=-1e18;
    for(auto p:comp){
        int sum=0;
        for(auto x:p)
            sum+=c[x];
        int ans=-1e18;
        int m=p.size();
        if(sum<=0){
            for(int j=0;j<m;j++){
                int ss=0;
                int id=j;
                for(int xx=0;xx<min(m,k);xx++){
                    ss+=c[p[id%m]];
                    ans=max(ans,ss);
                    id++;
                }
            }
        }
        else{
            ans=k/m*sum;
            int temp=-1e18;
            for(int j=0;j<m;j++){
                int ss=0;
                int id=j;
                for(int xx=0;xx<k%m;xx++){
                    ss+=c[p[id%m]];
                    temp=max(temp,ss);
                    id++;
                }
            }
            ans+=temp;
        }
        fans=max(fans,ans);
    }
    cout<<fans;
    return 0;
}

Compiler output:
a.cc: In function 'void dfs(long long int)':
a.cc:38:5: error: reference to 'visit' is ambiguous
   38 |     visit[i]=1;
      |     ^~~~~
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:35:21: note:                 'long long int visit [5005]'
   35 | int p[5005],c[5005],visit[5005];
      |                     ^~~~~
a.cc:40:9: error: reference to 'visit' is ambiguous
   40 |     if(!visit[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:35:21: note:                 'long long int visit [5005]'
   35 | int p[5005],c[5005],visit[5005];
      |                     ^~~~~
a.cc: In function 'int32_t main()':
a.cc:54:13: error: reference to 'visit' is ambiguous
   54 |         if(!visit[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:35:21: note:                 'long long int visit [5005]'
   35 | int p[5005],c[5005],visit[5005];
      |                     ^~~~~