Language: C++

  #pragma GCC optimize("Ofast")
  #pragma GCC target("avx,avx2,fma")
  #pragma GCC optimization ("unroll-loops")
  #include <bits/stdc++.h>
  using namespace std;
  //---------------------------------//
  void __print(int x) {cerr << x;}
  void __print(long x) {cerr << x;}
  void __print(long long x) {cerr << x;}
  void __print(unsigned x) {cerr << x;}
  void __print(unsigned long x) {cerr << x;}
  void __print(unsigned long long x) {cerr << x;}
  void __print(float x) {cerr << x;}
  void __print(double x) {cerr << x;}
  void __print(long double x) {cerr << x;}
  void __print(char x) {cerr << '\'' << x << '\'';}
  void __print(const char *x) {cerr << '\"' << x << '\"';}
  void __print(const string &x) {cerr << '\"' << x << '\"';}
  void __print(bool x) {cerr << (x ? "true" : "false");}

  template<typename T, typename V>
  void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
  template<typename T>
  void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
  void _print() {cerr << "]\n";}
  template <typename T, typename... V>
  void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
  #ifndef ONLINE_JUDGE
  #define deb(x...) cerr << "[" << #x << "] = ["; _print(x)
  #else
  #define deb(x...)
  #endif
 //------------------------------//

  typedef long long int ll;
  typedef pair<int,int> pi;
  typedef pair<ll,ll> pll;
  typedef vector<int> vi;
  typedef vector<ll> vll;
  typedef vector<pi> vpi;
  typedef vector<pll> vpll;

  #define  ff           first
  #define  ss           second
  #define  pb           push_back
  #define  IOS          ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  #define  all(x)       (x).begin(), (x).end()
  #define  ps(x,y)      fixed<<setprecision(y)<<x
  #define  rep(i,a,b)   for(ll i=a ; i<b ; ++i)

  const ll MOD = 1e9 + 7;
  const ll INF = 1e18;
  const ll maxn = 5000;
  vi vis(maxn,0);
  vi p(maxn);
  vi c(maxn);
  vll nw;
  ll sum=0;
  void dfs(int src){
     vis[src]=1;
     nw.pb(c[src-1]);
     sum += c[src-1];
     if(!vis[p[src-1]])
       dfs(p[src-1]);
  } 
  
  ll cal(ll p){
	  int i=0,j=0;
	  ll ans=INT_MIN;
	  int n=nw.size();
	  ll sum1=0;
	  while(j<n){
	  	  sum1 += nw[j];
	  	  while(j-i+1>p){
	  	  	 sum1 -= nw[i];
	  	  	 i++;
	  	  }
  	  	 if(j-i+1<=p)
  	    ans = max(ans,sum1);	
	  	  j++;
	  }
	  return ans;	
  }

  void solve(){
	  int n,k;
	  cin >> n >> k;
	  for(int i=0;i<n;i++)cin >> p[i];
	  for(int i=0;i<n;i++)cin >> c[i];
	  ll ans=INT_MIN;
	  for(int i=0;i<n;i++){
	  	 if(!vis[i+1]){
	  	 	sum=c[i];
	  	 	vis[i+1]=1;
	  	 	nw.pb(c[i]);
	  	 	if(!vis[p[i]])
	  	     dfs(p[i]);
	  	     deb(nw);
	  	 	ll tempans=0;
	  	 	int o = nw.size();
	  	   for(int j=0;j<o;j++){
	  	   	nw.pb(nw[j]);
	  	   }
	  	 	if(sum > 0){
	  	 		tempans = (max(0LL,(ll)(k/o)))*sum;
			   ll p = k - max(0LL,(ll)(k/o))*o;
				tempans += cal(p);
			}
			else{
				tempans += max(0,cal(1LL));
			}
	  	 	nw.clear();
			ans =max(ans,tempans);
	  	 }
	  }
	  cout << ans;
	  		
  }

  int main(){
    IOS
    	solve();
    //cerr <<endl <<"[ Time : " << (float)clock() / CLOCKS_PER_SEC << " secs ]" << endl;
  }

// integer overflow
// remember to clear visited array
// take input as vector<string> while using grid
// don't assign after modifying

Compiler output:
a.cc: In function 'void solve()':
a.cc:110:47: error: no matching function for call to 'max(int, ll)'
  110 |                                 tempans += max(0,cal(1LL));
      |                                            ~~~^~~~~~~~~~~~
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:4:
/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:110:47: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'll' {aka 'long long int'})
  110 |                                 tempans += max(0,cal(1LL));
      |                                            ~~~^~~~~~~~~~~~
/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:110:47: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  110 |                                 tempans += max(0,cal(1LL));
      |                                            ~~~^~~~~~~~~~~~