Language: C++

#include<bits/stdc++.h>
#define ll long long
#define dbg(x) ;
#define N 300005
#define M 1000000007
#define pii pair<ll,ll>
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

ll n,k;
ll p[N];
ll c[N];
int vis[5002];

main()
{
    ll a=29544642561,b=29507023469;
    //cout<<(a>=b)<<endl;
    cin>>n>>k;
    for(ll i=1;i<=n;i++)   cin>>p[i];
    for(ll i=1;i<=n;i++)   cin>>c[i];

    ll ans=-1e18;
    for(ll i=1;i<=n;i++)
    {
//        cout<<endl<<endl;
//        dbg(i);
        vector<ll>now;
        vector<ll>cs;
        vector<ll>go,cyc;

        ll cur=i;
        ll al=0;
        memset(vis,0,sizeof vis);

        while(now.size()<k)
        {


            cur=p[cur];

            if(vis[cur]==4)
                break;
            vis[cur]++;

            go.push_back(cur);
            now.push_back(c[cur]);
            al+=c[cur];
            cs.push_back(al);
            if(vis[cur]==4)
                cyc.push_back(c[cur]);

        }
//        if(i==5)
//        {
//            for(ll u:go)
//                cout<<u<<' ';
//            cout<<endl;
//            //return 0;
//        }

        if(now.size()==k)
        {
            for(ll u:cs)
            {
                ans=max(ans,u);
            }
        }
        else
        {
            for(ll u:cs)
            {
                ans=max(ans,u);
            }
            ll sz=cyc.size();
            //dbg(sz);
            ll al=cs[cs.size()-cyc.size()-1];
           // dbg(al);
            ans=max(ans,al);
            ll cycsum=0;
            for(ll u:cyc)
            {
                cycsum+=u;
            }
            dbg(cycsum);
            ll koyta=max(0LL,-1+(k-(cs.size()-cyc.size()))/sz);
            ll tot=(cs.size()-cyc.size())+koyta*sz;

            ll baki=k-tot;

            dbg(koyta);
            dbg(baki);
            //cout<<baki<<endl;
            cycsum*=koyta;
            cycsum+=al;
            dbg(cycsum);
            for(int i=0;i<sz;i++)
                cyc.push_back(cyc[i]);


            for(ll i=0;i<baki;i++)
            {
                cycsum+=cyc[i];
               // cout<<i<<' '<<cycsum<<endl;
                ans=max(ans,cycsum);
            }
        }
        //cout<<i<<' '<<ans<<endl;
    }
    cout<<ans;

}


Compiler output:
a.cc:15:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   15 | main()
      | ^~~~
a.cc: In function 'int main()':
a.cc:86:25: error: no matching function for call to 'max(long long int, long long unsigned int)'
   86 |             ll koyta=max(0LL,-1+(k-(cs.size()-cyc.size()))/sz);
      |                      ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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:86:25: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'long long unsigned int')
   86 |             ll koyta=max(0LL,-1+(k-(cs.size()-cyc.size()))/sz);
      |                      ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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:86:25: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   86 |             ll koyta=max(0LL,-1+(k-(cs.size()-cyc.size()))/sz);
      |                      ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~