Language: C++

#if loc||local
#define _GLIBCXX_DEBUG
#endif

#include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(ll i=0;i<(n);++i)
using ll = int_fast64_t;
using pll = pair<ll,ll>;
constexpr ll INF = 1LL<<60;
constexpr ll MOD = 1e9+7;
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}return 0;}
#if loc||local
template<class T>void dump(T&& t){cerr<<t<<endl;}
template<class T,class... Ts> void dump(T&& h, Ts&&... t){cerr<<h<<", ";dump(forward<Ts>(t)...);}
#else
void dump(){}
template<class T,class... Ts> void dump(T&& h, Ts&&... t){}
#endif
template<class T> istream &operator>>(istream&is,vector<T>&v){for(auto &elemnt:v)is>>elemnt;return is;}
template<class T,class U> istream &operator>>(istream&is,pair<T,U>&p){is>>p.first>>p.second;return is;}
template<class T> ostream &operator<<(ostream& os,vector<T>const& v){for(auto const& vi:v)os<<vi<<" ";return os;}
template<class T,class U> ostream &operator<<(ostream& os,pair<T,U>const& p){os<<p.first<<","<<p.second;return os;}
template<class T>vector<T> vec(size_t a){return vector<T>(a);}
template<class T, class... Ts>auto vec(size_t a, Ts... ts){return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...));}
struct union_find{
  size_t n;
  vector<int> par;
  size_t group;
  union_find(){}
  union_find(size_t _n):n(_n),group(_n){
    par.resize(n,-1);
  }
  void reset(size_t _n){
    n = _n;
    group = n;
    par.clear();
    par.resize(n,-1);
  }
  int root(int k){
    if(par[k]<0)return k;
    return par[k] = root(par[k]);
  }
  int size(int k){
    k = root(k);
    return -par[k];
  }
  auto operator[](int k){
    return size(k);
  }
  bool unite(int a,int b){
    a = root(a);
    b = root(b);
    if(a==b)return false;
    if(size(a)<size(b))swap(a,b);
    group--;
    par[a] += par[b];
    par[b] = a;
    return true;
  }
  auto operator()(int a,int b){
    return unite(a,b);
  }
  bool same(int a,int b){
    return root(a) == root(b);
  }
};
int main(){
return 1;
  ll n,k;
  cin>>n>>k;
  vector<ll> p(n),c(n);
  cin>>p>>c;
  for(auto& pi:p)pi--;

  ll ans = -INF;
  rep(i,n){
    int cur = i;
    int _k = k;
    ll tmp = 0;
    vector<int> ord(n,-1);
    int od = 1;
    while(_k--){
      tmp += c[p[cur]];
      chmax(ans,tmp);
      ord[p[cur]] = od++;
      cur = p[cur];
      if(ord[p[cur]]!=-1)break;
    }
    if(_k==-1)continue;
    int loop_s = p[cur];
    ll loop_len = ord[cur]-ord[s];
  }

}
signed _main(){

  ll n,k;
  cin>>n>>k;
  vector<ll> p(n),c(n);
  cin>>p>>c;
  for(auto& pi:p)pi--;
  
  ll ans = -INF;
  
  auto simu = [&](int s){
    vector<int> ord(n,-1);
    vector<ll> point(n,0);
    int par = -1;
    int cur = s;
    int num = 0;
    ll _k = k;
    ll tmp = 0;
    while(_k--){
      if(ord[p[cur]]!=-1)break;
      ord[p[cur]] = num++;
      point[p[cur]] = c[p[cur]]+point[cur];
      tmp = point[p[cur]];
      chmax(ans,tmp);
      par = cur;
      cur = p[cur];
    }
    dump(s);dump(ord);dump(point);
    ll looplen = k-ord[cur];
    ll ritoku = 0;
    int now = p[cur];
    while(now!=cur){
      ritoku += c[now];
      now = p[now];
    }
    ritoku += c[now];
    dump(looplen,ritoku);
    if(ritoku>0){
      ll loopnum = (k-ord[par])/looplen;
      tmp += (loopnum-1)*ritoku;
      chmax(ans,tmp);
      ll nokori = (k-ord[par])%looplen;
      dump(nokori,ord[par],loopnum);
      while(nokori--){
        tmp += c[p[cur]];
        chmax(ans,tmp);
        cur = p[cur];
      }
    }
    chmax(ans,tmp);
  };
  rep(i,n)simu(i);
  cout<<(ans)<<endl;
  

}


Compiler output:
a.cc: In function 'int main()':
a.cc:94:32: error: 's' was not declared in this scope
   94 |     ll loop_len = ord[cur]-ord[s];
      |                                ^
a.cc: In function 'int _main()':
a.cc:153:1: warning: no return statement in function returning non-void [-Wreturn-type]
  153 | }
      | ^