Language: C++

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define MOD ll(1e9+7)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cerr<<#x<<": "<<x<<endl

template<class T>
vector<T> cumsum1d(const vector<T> &v){
    int size = v.size();
    vector<T> cum(size+1,0);
    for(int i = 0; i < size; i++){
        cum[i+1] = v[i] + cum[i];
    }
    return cum;
}

int main() {
    ll n, num;
    cin >> n >> num;

    vector<ll> p(n), c(n);
    for(int i = 0; i < n; i++){
        cin >> p[i];
        p[i]--;
    }
    for(int i = 0; i < n; i++){
        cin >> c[i];
    }
    vector<ll> group(n, -1);
    vector<vector<ll>> loops(n);
    ll group_id = 0;
    for(int i = 0; i < n; i++){
        ll idx = i;
        if(group[idx] != -1){continue;}

        while(group[idx] == -1){
            group[idx] = group_id;
            loops[group_id].push_back(c[idx]);
            idx = p[idx];
        }
        group_id++;
    }

    for(int i = 0; i < group_id; i++){
        int lim = loops[i].size();
        for(int j = 0; j < lim; j++){
            loops[i].push_back(loops[i][j]);
        }
    }

    vector<vector<ll>> loops2(group_id);
    for(int i = 0; i < group_id; i++){
        loops2[i] = cumsum1d(loops[i]);
    }

    vector<ll> loop_sum(group_id, 0);
    for(int i = 0; i < group_id; i++){
        loop_sum[i] = loops2[i][loops[i].size()/2];
    }

    ll ans = -1e9-1;
    for(int i = 0; i < group_id; i++){
        int size =  loops[i].size()/2;
        for(int j = 0; j < size; j++){
            for(int k = j+1; k < j+1 + size; k++){
                if(num < k-j){continue;}
                ll tmpsum = loops2[i][k] - loops2[i][j];
                ans = max(ans, tmpsum);
                if(loop_sum[i] > 0){
                    ans = max(ans, tmpsum + (num - (k-j))/(loops[i].size()/2)*loop_sum[i]);
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:73:30: error: no matching function for call to 'max(ll&, long long unsigned int)'
   73 |                     ans = max(ans, tmpsum + (num - (k-j))/(loops[i].size()/2)*loop_sum[i]);
      |                           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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:73:30: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'long long unsigned int')
   73 |                     ans = max(ans, tmpsum + (num - (k-j))/(loops[i].size()/2)*loop_sum[i]);
      |                           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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:73:30: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   73 |                     ans = max(ans, tmpsum + (num - (k-j))/(loops[i].size()/2)*loop_sum[i]);
      |                           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~