Language: C++

#include <bits/stdc++.h>

#define int long long
#define double long double
#define ff first
#define ss second
#define endl '\n'
#define ii pair<int, int>
#define mp make_pair
#define mt make_tuple
#define DESYNC                      \
  ios_base::sync_with_stdio(false); \
  cin.tie(0);                       \
  cout.tie(0)
#define pb push_back
#define vi vector<int>
#define vii vector<ii>
#define all(x) x.begin(), x.end()
#define EPS 1e-9
#define INF 1e18
#define ROOT 1
#define M 1000000007
#define curtime chrono::steady_clock::now().time_since_epoch().count
#define rep(i, beg, n, s) for (int i = beg; i < n; i += s)
const double PI = acos(-1);

using namespace std;

inline int mod(int n, int m = M)
{
  int ret = n % m;
  if (ret < 0)
    ret += m;
  return ret;
}

int exp(int n, int k)
{
  if (k == 0)
    return 1;
  if (k == 1)
    return n;
  int ax = exp(n, k / 2);
  ax = mod(ax * ax);
  if (k % 2)
    ax = mod(ax * n);
  return ax;
}

int gcd(int a, int b)
{
  if (a == 0)
    return b;
  else
    return gcd(b % a, a);
}

int v[5005], p[5005];
int vis[5005], dep[5005], cst[5005];

int start, cost, size;
void go(int u, int d, int c)
{
  //cout << "at " << u << " " << d << " " << c << endl;
  vis[u] = 1;
  dep[u] = d;
  cst[u] = c;
  if (vis[p[u]] == 1)
  {
    start = p[u];
    size = d - dep[p[u]];
    cost = c - cst[p[u]] + v[p[u]];
    return;
  }
  go(p[u], d + 1, c + v[p[u]]);
  vis[u] = 2;
}

//#define MULTIPLE_TEST_CASE
void solution()
{
  int n, k;
  cin >> n >> k;
  int ans = -INF;
  for (int i = 1; i <= n; i++)
    cin >> p[i];
  for (int i = 1; i <= n; i++)
    cin >> v[i];

  for (int i = 1; i <= n; i++)
  {
    //start at i
    start = -1;
    cost = -1;
    size = -1;

    for (int i = 1; i <= n; i++)
    {
      vis[i] = 0;
      dep[i] = -1;
      cst[i] = -INF;
    }

    go(i, 0, v[i]);

    //cout << i << " " << start << " " << cost << " " << size << endl;

    for (int j = 1; j <= n; j++)
    {
      if (dep[j] > 0 && dep[j] <= k)
      {
        //cout << "considering " << j << " " << cst[j] - v[i] << endl;
        ans = max(ans, cst[j] - v[i]);
      }
    }

    if (start == -1)
      continue;
    if (dep[start] >= k)
      continue;

    // take cycle
    if (cost < 0)
      continue;

    int newk = k;
    newk -= dep[start];

    int cur = cst[start] - v[i];
    int times = (size) / newk;
    cur += times * cost;
    newk -= (size)*times;

    ans = max(ans, cur);
    int pos = start;
    while (newk--)
    {
      cur += v[p[pos]];
      pos = p[pos];
      ans = max(ans, cur);
    }
  }
  cout << ans << endl;
}

int32_t main()
{
  //DESYNC;
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int t = 1;
#ifdef MULTIPLE_TEST_CASE
  cin >> t;
#endif
  while (t--)
    solution();
}


Compiler output:
a.cc: In function 'void go(long long int, long long int, long long int)':
a.cc:71:5: error: reference to 'size' is ambiguous
   71 |     size = d - dep[p[u]];
      |     ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:61:18: note:                 'long long int size'
   61 | int start, cost, size;
      |                  ^~~~
a.cc: In function 'void solution()':
a.cc:95:5: error: reference to 'size' is ambiguous
   95 |     size = -1;
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:61:18: note:                 'long long int size'
   61 | int start, cost, size;
      |                  ^~~~
a.cc:130:18: error: reference to 'size' is ambiguous
  130 |     int times = (size) / newk;
      |                  ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:61:18: note:                 'long long int size'
   61 | int start, cost, size;
      |                  ^~~~
a.cc:132:14: error: reference to 'size' is ambiguous
  132 |     newk -= (size)*times;
      |              ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:61:18: note:                 'long long int size'
   61 | int start, cost, size;
      |                  ^~~~