Language: C++

#include <bits/stdc++.h>
using namespace std;

long long visit[5005], P[5005], C[5005];
vector<long long> X;

long long solve(int k)
{
    int sz = X.size();

    for (int i=1; i<sz; i++)
        X[i] += X[i-1];
    
    long long ans = - (long long) 1e18;
    long long rem = k % sz;
    long long pre = 1LL * (k / sz) * X.back();

    for (int i=0; i<sz; i++)
        ans = max(ans, X[i]);
    
    ans = max(ans, pre);

    for (int i=0; i<sz; i++)
        for (int j=i; j<sz; j++)
        {
            long long curr;
            int len = j - i + 1;

            if (len <= rem)
                curr = max(0LL, pre);
            else
            {
                if (k < sz)
                    continue;
                
                curr = max(0LL, pre - X.back());
            }

            ans = max(ans, X[j] - (i == 0 ? 0 : X[i-1]) + curr);
        }
    
    for (int i=0; i<sz; i++)
        for (int j=i+1; j<sz; j++)
        {
            long long curr;
            int len = i + 1 + sz - j;

            if (len <= rem)
                curr = max(0LL, pre);
            else
            {
                if (k < sz)
                    continue;
                
                curr = max(0LL, pre - X.back());
            }

            ans = max(ans, X.back() - X[j-1] + X[i] + curr);
        }

    return ans;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL), cout.tie(NULL);
 
    int n, k;
    cin >> n >> k;

    for (int i=1; i<=n; i++)
        cin >> P[i];
    
    for (int i=1; i<=n; i++)
        cin >> C[i];

    long long ans = - (long long) 1e18;
    
    for (int i=1; i<=n; i++)
    {
        if (visit[i])
            continue;
        
        int x = i;
        X.clear();

        while (!visit[x])
        {
            visit[x] = 1;
            X.push_back(C[x]);
            x = P[x];
        }

        ans = max(ans, solve(k));
    }

    cout << ans << "\n";
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:81:13: error: reference to 'visit' is ambiguous
   81 |         if (visit[i])
      |             ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc:1:
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:4:11: note:                 'long long int visit [5005]'
    4 | long long visit[5005], P[5005], C[5005];
      |           ^~~~~
a.cc:87:17: error: reference to 'visit' is ambiguous
   87 |         while (!visit[x])
      |                 ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:4:11: note:                 'long long int visit [5005]'
    4 | long long visit[5005], P[5005], C[5005];
      |           ^~~~~
a.cc:89:13: error: reference to 'visit' is ambiguous
   89 |             visit[x] = 1;
      |             ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:4:11: note:                 'long long int visit [5005]'
    4 | long long visit[5005], P[5005], C[5005];
      |           ^~~~~