Language: C++

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i=0; i<n; i++)
#define pb push_back
#define int long long


int N, K;
int P[5010], C[5010];
int visit[5010];

signed main() {
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> N >> K;
    rep(i, N) {
        int p; cin >> p;
        P[i] = p-1;
    }
    rep(i, N) cin >> C[i];
    int ans = -1000000000000000000;
    rep(i, N) ans = max(ans, C[i]);
    rep(i, N) {
        rep(j, 5010) visit[j] = 0;
        int now = i;
        vector<int> l;
        visit[i] = 1;
        rep(j, N+10) {
            int nex = P[now];
            l.pb(C[nex]);
            if (visit[nex]==1) break;
            now = nex;
        }
        int su = 0;
        rep(j, l.size()) su += l[j];
        if (su>0) {
            int cand = su*(K/l.size());
            int rem = K-K/l.size()*l.size();
            int cand2 = cand;
            rep(j, rem) {
                cand += l[j];
                cand2 = max(cand2, cand);
            }
            ans = max(ans, cand2);
        }
        else {
            int cand = l[0];
            int now = l[0];
            if (K<l.size()) {
                for (int j=1; j<K; j++) {
                    now += l[j];
                    cand = max(cand, now);
                }
                ans = max(ans, cand);
            }
            else {
                for (int j=1; j<K; j++) {
                    now += l[j];
                    cand = max(cand, now);
                }
                ans = max(ans, cand);
            }
        }
    }
    cout << ans << endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:23:22: error: reference to 'visit' is ambiguous
   23 |         rep(j, 5010) visit[j] = 0;
      |                      ^~~~~
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:10:5: note:                 'long long int visit [5010]'
   10 | int visit[5010];
      |     ^~~~~
a.cc:26:9: error: reference to 'visit' is ambiguous
   26 |         visit[i] = 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:10:5: note:                 'long long int visit [5010]'
   10 | int visit[5010];
      |     ^~~~~
a.cc:30:17: error: reference to 'visit' is ambiguous
   30 |             if (visit[nex]==1) break;
      |                 ^~~~~
/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:10:5: note:                 'long long int visit [5010]'
   10 | int visit[5010];
      |     ^~~~~