Language: C++

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define endl '\n'

void solvetask(){
    int n,k; cin >> n >> k;
    vector<int> p(n), c(n), vis(n, 0);
    for (int i=0; i<n; i++){
        cin >> p[i];
        p[i]--;
    }
    for (int i=0; i<n; i++) cin >> c[i];
    long long ans = -1e18;
    for (int i=0; i<n; i++){
        if (vis[i]) continue;
        vis[i] = 1;
        vector<int> cycle = {i};
        int x = i;
        long long sum = c[i];
        
        while(p[x] != cycle[0]){
            cycle.push_back(p[x]);
            x = p[x];
            vis[x] = 1;
            sum += c[x];
        }
        ans = max(ans, k / cycle.size() * sum);
        for (int j=0; j<cycle.size(); j++){
            long long temp = 0;
            for (int kk=1; kk<cycle.size(); kk++){
                temp += c[cycle[(j + kk)%cycle.size()]];
                ans = max({ans, temp, temp + (k - kk) / cycle.size() * sum});
            }
        }
    }
    cout << ans << endl;
}
//1 3 4 0 2
//0 -> 1 -> 3

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t=1; //cin >> t;
    while(t--) solvetask();
}

Compiler output:
a.cc: In function 'void solvetask()':
a.cc:32:18: error: no matching function for call to 'max(long long int&, long long unsigned int)'
   32 |         ans = max(ans, k / cycle.size() * sum);
      |               ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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:32:18: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'long long unsigned int')
   32 |         ans = max(ans, k / cycle.size() * sum);
      |               ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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:32:18: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   32 |         ans = max(ans, k / cycle.size() * sum);
      |               ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:37:26: error: no matching function for call to 'max(<brace-enclosed initializer list>)'
   37 |                 ans = max({ans, temp, temp + (k - kk) / cycle.size() * sum});
      |                       ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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:   candidate expects 2 arguments, 1 provided
/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, 1 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:   template argument deduction/substitution failed:
a.cc:37:26: note:   deduced conflicting types for parameter '_Tp' ('long long int' and 'long long unsigned int')
   37 |                 ans = max({ans, temp, temp + (k - kk) / cycle.size() * sum});
      |                       ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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:   candidate expects 2 arguments, 1 provided