Language: C++

#include <bits/stdc++.h>
using namespace std;
int main(){
    long long N, K;
    cin >> N >> K;
    vector<long long> 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];
    int i = 0;
    i = P[i];
    long long ans = -1e18;
    vector<int> used(N, 0);
    vector<vector<long long>> vec(N);
    int cnt = 0;
    for(int i = 0; i < N; i++){
        if(used[i] == 1) continue;
        int flag = 0;
        int ind = i, ind2 = i;
        while(flag == 0 || ind != ind2){
            flag = 1;
            vec[cnt].push_back(C[ind]);
            ind = P[ind];
            used[ind] = 1;
        }
        cnt++;
    }
    vector<vector<long long>> memo(cnt, vector<long long>(N, -1e18));
    for(int i = 0; i < cnt; i++){
        long long temp = 0;
        long long res = 0;
        for(int j = 0; j < vec[i].size(); j++){
            long long t = 0;
            for(int k = 0; k < vec[i].size(); k++){
                t += vec[i][(j + k) % vec[i].size()];
                memo[i][k] = max(memo[i][k], t);
            }
            temp += vec[i][j];
        }
        if(temp > 0) res += K / vec[i].size() * temp;
        else{
            long long r = -1e18;
            for(int j = 0; j < min(K, vec[i].size()); j++){
                r = max(r, memo[i][j]);
            }
            res += r;
            ans = max(res, ans);
            continue;
        }
        long long t = -1e18;
        for(int j = 0; j < K % vec[i].size(); j++){
            t = max(t, memo[i][j]);
        }
        res += t;
        ans = max(res, ans);
    }
    cout << ans << endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:45:35: error: no matching function for call to 'min(long long int&, std::vector<long long int>::size_type)'
   45 |             for(int j = 0; j < min(K, vec[i].size()); j++){
      |                                ~~~^~~~~~~~~~~~~~~~~~
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:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:45:35: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'})
   45 |             for(int j = 0; j < min(K, vec[i].size()); j++){
      |                                ~~~^~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281: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:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:45:35: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   45 |             for(int j = 0; j < min(K, vec[i].size()); j++){
      |                                ~~~^~~~~~~~~~~~~~~~~~