Language: C++

#include<bits/stdc++.h>
#include<cstring>
//#define Mfc_Tanzim
#define ll long long
#define ull unsigned long long
#define pb push_back
#define ff first
#define ss second
#define all(v) (v.begin(), v.end())
#define pi acos(-1.0)
#define FastRead ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
#define bug(a) cerr << #a << " : " << a << endl
#define pq dp
using namespace std;
const ll mx = 4444444444;

ll posx[] = {1,-1, 0, 0};
ll posy[] = {0, 0, 1,-1};
vector<ll>v, v2;
ll ret = INT_MIN;
map<ll, bool>vis;

void Dfs(ll n, ll k)
{
    vector<ll>res;
    vis.clear();
    ll r = 0;
    while(!vis[n-1]){
              r+=v2[n-1];
              vis[n-1] = true;
              res.pb(v2[n-1]);
              n = v[n-1];
    }
    ll ans = INT_MIN;
    for(ll i=0; i<res.size(); i++){
             if(i){
                   res[i]+=res[i-1];
             }
             if(i+1<=k){
                   ans = max(ans, res[i]);
             }
             else{
                   break;
             }
             if(r){
                   ans = max(ans, res[i]+(k-i-1)/res.size()*r);
             }
    }
    ret = max(ret, ans);
}

int main()
{
    FastRead

#ifdef Mfc_Tanzim
    freopen("input.txt","r", stdin);
    // freopen("output.txt","w", stdout);
#endif /// Mfc_Tanzim

    ll t=1, n, k, m, a, b, c, d;
    //cin >> t;

    while(t--){
              cin >> n >> k;
              v.resize(n);
              v2.resize(n);
              for(auto &i: v){
                       cin >> i;
              }
              for(auto &i: v2){
                       cin >> i;
              }
              for(ll i=0; i<n; i++){
                       Dfs(v[i], k);
              }
              cout << ret << endl;
    }
    return 0;
}





Compiler output:
a.cc: In function 'void Dfs(long long int, long long int)':
a.cc:46:29: error: no matching function for call to 'max(long long int&, long long unsigned int)'
   46 |                    ans = max(ans, res[i]+(k-i-1)/res.size()*r);
      |                          ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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:46:29: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'long long unsigned int')
   46 |                    ans = max(ans, res[i]+(k-i-1)/res.size()*r);
      |                          ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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:46:29: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   46 |                    ans = max(ans, res[i]+(k-i-1)/res.size()*r);
      |                          ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~