Language: C++

#include <bits/stdc++.h>

#define endl "\n"
#define ll long long
#define ios ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);

using namespace std;

const int N = 2e4;
const ll inf = 2e10;
int n;
vector <int> g[N], used(N, 0), p(N, 0), v;
ll c[N], k;

void dfs(int ve) {
    used[ve] = 1;
    v.push_back(ve);
    for (int i = 0; i < g[ve].size(); ++ i) {
        int to = g[ve][i];
        if (used[to] == 0) {
            dfs(to);
        }
    }
}

ll ans = -inf;

ll ff(vector <ll> &pop) {
    ll crug = 0;
    vector <ll> cop = pop;
    for (int i = 1; i < pop.size(); ++ i) {
       pop[i] += pop[i - 1];
    }
    ll nowans = 0;
    if (pop.back() < 0) {
        crug = 0;
        ll mas = pop[0];
        for (int i = 0; i < min(1ll * pop.size(), k); ++ i) {
            mas = max(mas, pop[i]);
        }
        return mas;
    }
    else {
        crug = (ll)(pop.size() / k) - 1;
    }
    nowans += crug * pop.back();
    ll maxx = 0;
    ll sum = 0;
    for (int i = 0; i < k - crug * pop.size(); ++ i) {
        sum += cop[i % pop.size()];
        maxx = max(maxx, sum);
    }
    nowans += maxx;
    return nowans;
}

void count(vector <int> &now) {
    for (int i = 0; i < now.size(); ++ i) {
        vector <ll> df;
        for (int j = i; j < now.size(); ++ j) {
            df.push_back(c[now[j]]);
        }
        for (int j = 0; j < i; ++ j) {
            df.push_back(c[now[j]]);
        }
        ll sdf = ff(df);
        ans = max(ans, sdf);
    }
}

int main() {
    ios
    cin >> n >> k;
    for (int i = 1; i <= n;  ++ i) {
        cin >> p[i];
        g[i].push_back(p[i]);
    }
    for (int i = 1; i <= n; ++ i) {
        cin >> c[i];
    }
    for (int i = 1; i <= n; ++ i) {
        if (used[i] == 0) {
            dfs(i);
            count(v);
            v.clear();
        }
    }
    cout << ans;
    return 0;
}


Compiler output:
a.cc: In function 'long long int ff(std::vector<long long int>&)':
a.cc:38:32: error: no matching function for call to 'min(long long unsigned int, long long int&)'
   38 |         for (int i = 0; i < min(1ll * pop.size(), k); ++ 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: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:38:32: note:   deduced conflicting types for parameter 'const _Tp' ('long long unsigned int' and 'long long int')
   38 |         for (int i = 0; i < min(1ll * pop.size(), k); ++ i) {
      |                             ~~~^~~~~~~~~~~~~~~~~~~~~
/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:38:32: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long unsigned int'
   38 |         for (int i = 0; i < min(1ll * pop.size(), k); ++ i) {
      |                             ~~~^~~~~~~~~~~~~~~~~~~~~