Language: C++

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
using ll = long long;

long long ncr(int n, int r) {
    if(r > n - r) r = n - r; // because C(n, r) == C(n, n - r)
    long long ans = 1;
    int i;

    for(i = 1; i <= r; i++) {
        ans *= n - r + i;
        ans /= i;
    }

    return ans;
}
ll mod(ll a,ll b,ll m){
    if(b==0)return 1;
    if(b%2==0){
        ll y =  mod(a,b/2,m);
        return (y*y)%m;
    }
    else{
        return ((a%m)*mod(a,b-1,m))%m;
    }
}

ll modinv(ll a,ll m){
return mod(a,m-2,m);
//works only for prime m.
//else calculate phi(m)-1.
}



int main()
{
    ll n,k;
    cin>>n>>k;
    vector<int>p(n+1),c(n+1);
    for(int i=1;i<=n;i++)cin>>p[i];
    for(int i=1;i<=n;i++)cin>>c[i];
    ll best=-9223372036854775808;
    for(int i=1;i<=n;i++){
        //start at index i;

        ll score=0;ll tracker=-9223372036854775808;

        ll moves=0;
        int index=i;ll lm=0;
        for(int j=1;j<=k;j++){
            //go to square p[i];
            if(p[index]==i){
                score+=c[i];
                moves=j;
               if(score>tracker){
                tracker=max(tracker,score);
                lm=j;
                }
                break;
            }
            else{
                score+=c[p[index]];
                index=p[index];
                moves=j;
                if(score>tracker){
                tracker=max(tracker,score);
                lm=j;
                }
            }


        }

        ll f=k/moves;
        score=f*score;
        ll q=k%moves;
        index=i;ll z=score;
        for(int j=0;j<q;j++){
            z+=c[p[index]];
                index=p[index];
                score=max(score,z);
        }
        best=max(best,score);


       best=max(best,max(0,tracker));



    }
    cout<<best;

    return 0;
}


Compiler output:
a.cc:44:14: warning: integer constant is so large that it is unsigned
   44 |     ll best=-9223372036854775808;
      |              ^~~~~~~~~~~~~~~~~~~
a.cc:48:32: warning: integer constant is so large that it is unsigned
   48 |         ll score=0;ll tracker=-9223372036854775808;
      |                                ^~~~~~~~~~~~~~~~~~~
a.cc: In function 'int main()':
a.cc:88:25: error: no matching function for call to 'max(int, ll&)'
   88 |        best=max(best,max(0,tracker));
      |                      ~~~^~~~~~~~~~~
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: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:88:25: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'll' {aka 'long long int'})
   88 |        best=max(best,max(0,tracker));
      |                      ~~~^~~~~~~~~~~
/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 /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:2:
/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:88:25: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   88 |        best=max(best,max(0,tracker));
      |                      ~~~^~~~~~~~~~~