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)
{
    for (int i=1; i<X.size(); i++)
        X[i] += X[i-1];
    
    long long ans = - (long long) 1e18;
    long long rem = k % X.size();
    long long pre = 1LL * (k / X.size()) * X.back();

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

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

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

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

            if (len <= rem)
                curr = max(0LL, pre);
            else
            {
                if (k < X.size())
                    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:79:13: error: reference to 'visit' is ambiguous
   79 |         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:85:17: error: reference to 'visit' is ambiguous
   85 |         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:87:13: error: reference to 'visit' is ambiguous
   87 |             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];
      |           ^~~~~