Language: C++

// @iamnifer
#include <iostream>
#include <set>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <stack>
#include <deque>
#include <queue>
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define elif else if
#define inf (ll)1e18
#define append push_back
#define pb push_back
#define str to_string
#define gcd __gcd
#define optimise cout.tie(0);cin.tie(0);ios_base::sync_with_stdio(0);
#define all(x) x.begin(),x.end()
#define len(x) x.size()
#define add insert
#define PI 3.14159265358979323846264338327950288419716939937510
#define lcm(a,b) a/gcd(a,b)*b
#define eps 0.0000000001
#define endl '\n'
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
//#pragma gcc optimize("no-stack-protector,tune=native")
//#pragma gcc optimize("sse,sse2,sse3,sse4,ssse3")
//#pragma gcc optimize("abm,mmx,avx,avx2,unroll-loops,fast-math,section-anchors")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef vector<vector<ll>>graph;
const ll MOD = (ll)1e9+7;//998244353
const ll maxN= (ll)5009;
ll p[maxN];
vector<bool>vis(maxN);
vector<ll>cur;
void dfs(ll v){
    vis[v]=true;
    cur.pb(v);
    if(!vis[p[v]]){
        dfs(p[v]);
    }
}
void solve()
{
    ll n,k;cin>>n>>k;
    for(ll i=1;i<=n;i++){
        cin>>p[i];
    }
    ll c[n+1];
    ll ans=0,cs,ff,mff,mff2;
    ll negs=0;
    for(ll i=1;i<=n;i++){
        cin>>c[i];
        if(c[i]<=0)negs++;
    }
    if(negs==n){
        cout<<*max_element(c+1,c+n+1);
        return;
    }
    for(ll i=1;i<=n;i++){
        cur.clear();
        vis.assign(n+1,false);
        dfs(i);
        cs=0;
        ff=0;mff=0;mff2=0;
        for(ll j=0;j<len(cur);j++){
            cs+=c[cur[j]];
            if(j<k%len(cur)){
                ff+=c[cur[j]];
                mff2=max(mff2,ff);
            }
            if(j<k)mff=max(mff,cs);
        }
        if(cs<0){
            ans=max(ans,mff);
        }
        else ans=max(ans,(k/len(cur))*cs+mff2);
    }
    cout<<ans;
}
int main()
{
    optimise
    ll multitest=0;
    ll t=1;
    if(multitest)cin>>t;
    while(t--)solve();
}

Compiler output:
a.cc: In function 'void solve()':
a.cc:84:21: error: no matching function for call to 'max(long long int&, long long unsigned int)'
   84 |         else ans=max(ans,(k/len(cur))*cs+mff2);
      |                  ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:2:
/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:84:21: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'long long unsigned int')
   84 |         else ans=max(ans,(k/len(cur))*cs+mff2);
      |                  ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/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,
                 from a.cc:6:
/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:84:21: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   84 |         else ans=max(ans,(k/len(cur))*cs+mff2);
      |                  ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~