Language: C++

#include<iostream>
#include<vector>
using namespace std;

long long N,K;
long long P[5001];
long long C[5001];

vector<long long> ele[5001];

void visit(long long i, long long v){
    if(i==v) return;
    ele[i].push_back(v);
    visit(i,P[v]);
}

void search(){
    long long i;
    for(i=1; i<=N; i++){
        ele[i].push_back(i);
        visit(i,P[i]);
    }
}

int main(){
    cin >> N >> K;
    long long i,j,v;
    for(i=1; i<=N; i++){
        cin >> P[i];
    }
    for(i=1; i<=N; i++){
        cin >> C[i];
    }
    search();
    long long ans=-1000000000000000010;
    long long sum,subsum, subsummax;
    for(i=1; i<=N; i++){
        sum=0;
        for(j=0; j<ele[i].size(); j++){
            sum+=C[ele[i][j]];
        }
        subsum=C[i];
        subsummax=C[i];
        for(j=1; j<ele[i].size()-1; j++){
            subsum+=C[ele[i][j]];
            subsummax=max(subsummax,subsum);
        }
        if(K<ele[i].size()){
            subsum=C[i];
            ans=max(ans,subsum);
            for(j=1; j<K; j++){
                subsum+=C[ele[i][j]];
                ans=max(ans,subsum);
            }
        }else{
            ans=max(ans,subsummax);
            ans=max(ans,sum);
            ans=max(ans,((K/ele[i].size())-1)*sum);
            ans=max(ans,((K/ele[i].size())-1)*sum+subsummax);
            subsum=((K/ele[i].size()))*sum;
            ans=max(ans,subsum);
            for(j=0; j<(K%ele[i].size()); j++){
                subsum+=C[ele[i][j]];
                ans=max(ans,subsum);
            }
        }
    }
        

    cout << ans << endl;
    system("pause");
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:58:20: error: no matching function for call to 'max(long long int&, long long unsigned int)'
   58 |             ans=max(ans,((K/ele[i].size())-1)*sum);
      |                 ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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:58:20: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'long long unsigned int')
   58 |             ans=max(ans,((K/ele[i].size())-1)*sum);
      |                 ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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
a.cc:59:20: error: no matching function for call to 'max(long long int&, long long unsigned int)'
   59 |             ans=max(ans,((K/ele[i].size())-1)*sum+subsummax);
      |                 ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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:59:20: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'long long unsigned int')
   59 |             ans=max(ans,((K/ele[i].size())-1)*sum+subsummax);
      |                 ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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