Language: C++

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

ll inf = 9223372036854775807;

int main()
{

    int N, K;
    cin >> N >> K;

    vector<ll> P(N);
    vector<ll> c(N);
    //vector<bool> visit(N,0);

    for (int i = 0; i < N; i++)
    {

        cin >> P[i];

        P[i]--;
    }

    for (int i = 0; i < N; i++)
    {

        cin >> c[i];
    }

    vector max_v(N, 0);

    for (int i = 0; i < N; i++)
    {

        vector<int> fmt(0);

        vector<bool> visit(N, 0);

        int a = i;

        int roop_i = 0;

        while (1)
        {
            if (visit[a] == 0)
            {

                visit[a] = 1;

                a = P[a];

                fmt.push_back(a);
            }
            else
            {

                int s = fmt.size();

                for (int p = 0; p < s; p++)
                {

                    if (fmt[p] == a)
                    {

                        roop_i = p;

                        break;
                    }
                }

                if (roop_i = !0)
                {

                    vector<ll> bl(roop_i + 1, 0);

                    ll bl_max = -inf;

                    for (int q = 1; q <= roop_i; q++)
                    {

                        bl[q] = l[q - 1] + c[fmt[q]];

                        bl_max = max(bl_max, bl[q]);

                        if (K == q)
                        {

                            max_v[i] = bl_max;
                        }
                    }

                    vector cl(s - roop_i, 0);

                    ll cl_max = 0;

                    ll cl_sum = c[fmt[roop_i]];

                    ll noa = K - roop_i;

                    if (noa > 0)
                    {

                        ll amari = noa % (s - roop_i);

                        ll amari_max = 0;

                        for (int q = 1; q < s - roop_i; q++)
                        {

                            cl[q] = l[q - 1] + c[fmt[q + roop_i]];

                            cl_max = max(cl_max, cl[q]);

                            if (amari == q)
                            {

                                amari_max = cl_max;
                            }
                        }

                        cl_sum += cl[s - roop_i - 1];

                        ll v = amari_max + (K / (s - roop_i)) * cl_sum + bl[roop_i];

                        v = max(v, amari_max + bl[roop_i])

                            v_max[i] = max(bl_max, v);
                    }

                    break;
                }
                else
                {
                    vector cl(s - roop_i, 0);

                    ll cl_max = -inf;

                    ll cl_sum = c[fmt[roop_i]];

                    ll noa = K - roop_i;

                    if (noa > 0)
                    {

                        ll amari = noa % (s - roop_i);

                        ll amari_max = 0;

                        for (int q = 1; q < s - roop_i; q++)
                        {

                            cl[q] = l[q - 1] + c[fmt[q + roop_i]];

                            cl_max = max(cl_max, cl[q]);

                            if (amari == q)
                            {

                                amari_max = cl_max;
                            }
                        }

                        cl_sum += cl[s - roop_i - 1];

                        ll v = amari_max + (K / (s - roop_i)) * cl_sum ;

                        v = max(v, amari_max)

                            v_max[i] = v;
                    }

                    break;
                }
            }
        }
    }



    ll ans=-inf;


    for(int i=0;i<N;i++){


        ans =max(v_max[i],ans);
    }


    cout << ans << endl;

    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:83:33: error: 'l' was not declared in this scope
   83 |                         bl[q] = l[q - 1] + c[fmt[q]];
      |                                 ^
a.cc:112:37: error: 'l' was not declared in this scope
  112 |                             cl[q] = l[q - 1] + c[fmt[q + roop_i]];
      |                                     ^
a.cc:114:41: error: no matching function for call to 'max(ll&, __gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&)'
  114 |                             cl_max = max(cl_max, cl[q]);
      |                                      ~~~^~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:114:41: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'})
  114 |                             cl_max = max(cl_max, cl[q]);
      |                                      ~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:114:41: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  114 |                             cl_max = max(cl_max, cl[q]);
      |                                      ~~~^~~~~~~~~~~~~~~
a.cc:127:59: error: expected ';' before 'v_max'
  127 |                         v = max(v, amari_max + bl[roop_i])
      |                                                           ^
      |                                                           ;
  128 | 
  129 |                             v_max[i] = max(bl_max, v);
      |                             ~~~~~                          
a.cc:154:37: error: 'l' was not declared in this scope
  154 |                             cl[q] = l[q - 1] + c[fmt[q + roop_i]];
      |                                     ^
a.cc:156:41: error: no matching function for call to 'max(ll&, __gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&)'
  156 |                             cl_max = max(cl_max, cl[q]);
      |                                      ~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:156:41: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'})
  156 |                             cl_max = max(cl_max, cl[q]);
      |                                      ~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:156:41: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  156 |                             cl_max = max(cl_max, cl[q]);
      |                                      ~~~^~~~~~~~~~~~~~~
a.cc:169:46: error: expected ';' before 'v_max'
  169 |                         v = max(v, amari_max)
      |                                              ^
      |                                              ;
  170 | 
  171 |                             v_max[i] = v;
      |                             ~~~~~             
a.cc:188:18: error: 'v_max' was not declared in this scope
  188 |         ans =max(v_max[i],ans);
      |                  ^~~~~