Language: C++

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define double long double
#define fo(a,b) for(int a=0;a<b;a++)
#define Sort(a) sort(a.begin(),a.end())
#define rev(a) reverse(a.begin(),a.end())
#define fi first
#define se second
#define sz size()
#define bgn begin()
#define en end()
#define pb push_back
#define pp() pop_back()
#define V vector
#define P pair
#define yuko(a) setprecision(a)
#define uni(a) a.erase(unique(a.begin(),a.end()),a.end())
#define Q queue
#define pri priority_queue
#define Pri priority_queue<int,vector<int>,greater<int>>
#define PriP priority_queue<P<int,int>,vector<P<int,int>>,greater<P<int,int>>>
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
#define all(a)  (a).begin(),(a).end()
#define elif else if

struct Union{
  vector<int> par;
  Union(int a){
  par=vector<int>(a,-1);
  }
  int find(int a){
    if(par[a]<0)
      return a;
    else
      return par[a]=find(par[a]);
  }
  bool same(int a,int b){
    return find(a)==find(b);
  }
  int Size(int a){
    return -par[find(a)];
  }
  void unite(int a,int b){
    a=find(a);
    b=find(b);
    if(a==b)
      return;
    if(Size(b)>Size(a))
      swap<int>(a,b);
    par[a]+=par[b];
    par[b]=a;
  }
};

signed main(){
  int a,b;
  cin>>a>>b;
  V<int> c(a);
  fo(i,a){
    cin>>c[i];
    c[i]--;
  }
  V<ll> d(a),f(a,1);
  fo(i,a)
  cin>>d[i];
  Union e(a);
  V<V<ll>> h;
  V<ll> h1;
  fo(i,a){
    if(f[i]){
      f[i]=0;
      V<ll> m;
      m.pb(d[i]);
      h1.pb(d[i]);
      ll g=c[i];
      m.pb(d[g]);
      f[g]=0;
      h1[h1.sz-1]+=d[g];
      e.unite(i,g);
      while(c[g]!=i){
        e.unite(g,c[g]);
        g=c[g];
        f[g]=0;
        m.pb(d[g]);
        h1[h1.sz-1]+=d[g];
      }
      h.pb(m);
    }
  }
  ll h2=h.sz;
  ll n=-inf;
  fo(i,h2){
    ll h3=h[i].sz;
    ll o=0;
    if(h1[i]>0&&b>=2*h3-1){
      o+=h1[i]*(b/h3-1);
    }
    ll ma=-inf,ma1;
    ll mi=inf,mi1;
    V<ll> h4(h3+1);
    fo(j,h3)
    h4[j+1]=h4[j]+h[i][j];
    for(int j=1;j<=min(b,h3);j++){
      for(int k=1;k<h3-j+2;k++){
        ll ma2=0,mi2=0;
        if(h1[i]>0&&b>h3){
          ma2=(j<=b%h3)*h1[i];
          mi2=(h3-j<=b%h3)*h1[i];
        }
        if(ma<h4[k+j-1]-h4[k-1]+ma2){
          ma=max(ma,h4[k+j-1]-h4[k-1]+ma2);
          ma1=j;
        }
        if(mi>=h4[k+j-1]-h4[k-1]-mi2&&j!=h3){
          mi=h4[k+j-1]-h4[k-1]-mi2;
          mi1=h3-j;
        }
      }
    }
    if(ma<h1[i]-mi){
      ma=max(ma,h1[i]-mi);
      ma1=mi1;
    }
    o+=ma;
    n=max(n,o);
  }
  cout<<n<<endl;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:95:9: error: 'inf' was not declared in this scope; did you mean 'ynf'?
   95 |   ll n=-inf;
      |         ^~~
      |         ynf
a.cc:107:23: error: no matching function for call to 'min(int&, ll&)'
  107 |     for(int j=1;j<=min(b,h3);j++){
      |                    ~~~^~~~~~
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:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:107:23: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'll' {aka 'long long int'})
  107 |     for(int j=1;j<=min(b,h3);j++){
      |                    ~~~^~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281: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:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:107:23: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  107 |     for(int j=1;j<=min(b,h3);j++){
      |                    ~~~^~~~~~
a.cc:116:11: error: 'ma1' was not declared in this scope; did you mean 'ma2'?
  116 |           ma1=j;
      |           ^~~
      |           ma2
a.cc:126:7: error: 'ma1' was not declared in this scope; did you mean 'mi1'?
  126 |       ma1=mi1;
      |       ^~~
      |       mi1