Language: C++

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

const int mn=1e4+5,Log=14;
int n,k,m,tmp_k;
int c[mn],p[mn],vis[mn];
long long ans=-2e18,sum[mn];
vector<int> a;

void dfs(int x)
{
    if(vis[x]) return ;
    vis[x]=1;
    a.push_back(x);
    dfs(p[x]);
}

int main()
{
    scanf("%d%d",&n,&k);
    tmp_k=k;
    for(int i=1;i<=n;++i) scanf("%d",p+i);
    for(int i=1;i<=n;++i) scanf("%d",c+i);
    int f=0;
    for(int i=1;i<=n;++i) if(c[i]>0) f=1;
    if(f) continue;
    else {
        for(int i=1;i<=n;++i) ans=max(ans,c[i]);
        cout<<ans;
        return 0;
    }
    for(int _i=1;_i<=n;++_i) {
        if(!vis[_i]) {
            k=tmp_k;
            a.clear();
            memset(sum,0,sizeof sum);
            a.push_back(0);
            dfs(_i);
            m=a.size()-1;
            for(int i=1;i<=m;++i) a.push_back(a[i]);
            m*=2;
            for(int i=1;i<=m;++i) sum[i]=sum[i-1]+c[a[i]];
            // for(int i=1;i<=m;++i) {
            //     st[0][i]=st[0][i-1]+c[a[i]];
            //     //sum[i]=sum[i-1]+c[a[i]];
            // }
            // for(int i=1;i<=m;++i) {
            //     st[0][i]=sum[i];
            // }
            // for(int i=1;i<Log;++i) for(int j=1;j<=m;++j) {
            //     if(j+(1<<i)<=m) continue;
            //     st[i][j]=max(st[i-1][j],st[i-1][j+(1<<i)]);
            // }
            m/=2;
            long long tmp_ans=0,tmp=-2e18;
            if(sum[m]>0) {
                if(k>=m) tmp_ans+=sum[m]*(k/m),k%=m;
            }
            else if(k>=m) k=m;
            for(int i=1;i<=m;++i) {
                long long t=sum[i-1];
                for(int j=1;j<=k;++j) {
                    t=max(t,sum[i+j-1]);
                }
                tmp=max(tmp,t-sum[i-1]);
            }
            tmp_ans+=tmp;
            ans=max(ans,tmp_ans);
        }
    }
    cout<<ans;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:26:11: error: continue statement not within a loop
   26 |     if(f) continue;
      |           ^~~~~~~~
a.cc:28:38: error: no matching function for call to 'max(long long int&, int&)'
   28 |         for(int i=1;i<=n;++i) ans=max(ans,c[i]);
      |                                   ~~~^~~~~~~~~~
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:28:38: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   28 |         for(int i=1;i<=n;++i) ans=max(ans,c[i]);
      |                                   ~~~^~~~~~~~~~
/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:28:38: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   28 |         for(int i=1;i<=n;++i) ans=max(ans,c[i]);
      |                                   ~~~^~~~~~~~~~