Language: C++

#include <bits/stdc++.h>
#define _itr ::iterator
#define forn(i, x, y) for (ll i = x; i < y; ++i)
#define ford(i, x, y) for (ll i = x; i > y; --i)
#define forne(i, x, y) for (ll i = x; i <= y; ++i)
#define forde(i, x, y) for (ll i = x; i >= y; --i)
#define rd(x) cin >> x; cin.ignore();
#define rdstr(x) getline(cin, x);
#define rdarr(x, y) for (ll i = 0; i < y; ++i) cin >> x[i];
#define rdmtr(x, y, z) for (ll i = 0; i < y; ++i) for (ll j = 0; j < z; ++j) cin >> x[i][j];
#define pb push_back

#define X first
#define Y second

#define MaxN 5007
#define MaxM 507
#define Mdl 1000000007

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
typedef ll arr[MaxN];

ll REPIT = 1;

const bool MQ = false;
ll n, k;
arr mv, so;
bool visit[MaxN];
vector< vi > comp;

void Move(ll cur)
{
    (comp.back()).push_back(so[cur]);
    visit[cur] = 1;
    if (!visit[mv[cur]]) Move(mv[cur]);
}

void Solve()
{
    cin >> n >> k;
    rdarr(mv, n);
    forn(i, 0, n) --mv[i];
    rdarr(so, n);
    fill(visit, visit + n, 0);
    forn(i, 0, n)
    {
        if (!visit[i])
        {
            comp.push_back({});
            Move(i);
        }
    }
    ll res = LLONG_MIN;
    for (vi ar : comp)
    {
        ll sum = 0;
        for (ll v : ar)
        {
            sum += v;
        }
        ll s = sum * (k / (ll)ar.size());
        ll sz = k % (ll)ar.size(), m = (ll)ar.size();
        vi pref;
        pref.push_back(0);
        ll max_s = LLONG_MIN, max_rem = LLONG_MIN;
        forn(i, 0, 2 * m) pref.push_back(pref.back() + ar[i % m]);
        for (ll i = 1; i <= m; ++i)
        {
            for (ll j = i - 1; j < m + i - 1; ++j)
            {
                if (i == sz) max_rem = max(max_rem, pref[j + 1] - pref[j - i + 1]);
                max_s = max(max_s, pref[j + 1] - pref[j - i + 1]);
            }
        }
        if (sum >= 0) res = max(res, max(max_s, s + max_rem));
        else  res = max(res, max_s);
    }
    cout << res;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    #ifdef OFFLINE
    freopen("INPUT.inp", "r", stdin);
    #endif
    if (MQ == true)
    {
        rd(REPIT);
    }
    while (REPIT--) Solve();
    cout.flush();
    #ifdef OFFLINE
    fclose(stdin);
    #endif
}


Compiler output:
a.cc: In function 'void Move(ll)':
a.cc:38:5: error: reference to 'visit' is ambiguous
   38 |     visit[cur] = 1;
      |     ^~~~~
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:32:6: note:                 'bool visit [5007]'
   32 | bool visit[MaxN];
      |      ^~~~~
a.cc:39:10: error: reference to 'visit' is ambiguous
   39 |     if (!visit[mv[cur]]) Move(mv[cur]);
      |          ^~~~~
/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:32:6: note:                 'bool visit [5007]'
   32 | bool visit[MaxN];
      |      ^~~~~
a.cc: In function 'void Solve()':
a.cc:48:10: error: reference to 'visit' is ambiguous
   48 |     fill(visit, visit + n, 0);
      |          ^~~~~
/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:32:6: note:                 'bool visit [5007]'
   32 | bool visit[MaxN];
      |      ^~~~~
a.cc:48:17: error: reference to 'visit' is ambiguous
   48 |     fill(visit, visit + n, 0);
      |                 ^~~~~
/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:32:6: note:                 'bool visit [5007]'
   32 | bool visit[MaxN];
      |      ^~~~~
a.cc:51:14: error: reference to 'visit' is ambiguous
   51 |         if (!visit[i])
      |              ^~~~~
/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:32:6: note:                 'bool visit [5007]'
   32 | bool visit[MaxN];
      |      ^~~~~