Language: C++

#include <bits/stdc++.h>
#include <iostream>
#include <functional>
#include <vector>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <math.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define rep1(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#define per1(i,a,n) for (int i=a;i>n;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define endl "\n"
#define pi 3.14159265358979323846264338327950288419716939937510
#define IOS cin.sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long
#pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll mod=1000000007;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
ll qpow(ll a,ll b){ll s=1;while(b>0){if(b%2==1){s=s*a;}a=a*a;b=b>>1;}return s;}
ll qpowmod(ll a, ll b, ll c){ll res, t;res=1;t=a%c;while(b){if(b & 1){res=res*t%c;}t=t*t%c;b>>=1;}return res;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline long long read(){long long k=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}return k*f;}
inline void write(long long x){if(x<0)x=-x,putchar('-');if(x>9)write(x/10);putchar(x%10+'0');}
// head
const int maxn=5005;
ll n,k,p[maxn],c[maxn],mx[maxn],ans;
bool vis[maxn];
vector<int> v;
void dfs(int x)
{
    vis[x]=1;
    v.push_back(c[p[x]]);
    if(!vis[p[x]])
        dfs(p[x]);
}
signed main()
{
    //IOS;
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    memset(mx,-63,sizeof(mx));
    ans=mx[0];
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        cin>>p[i];
    }
    for(int i=1;i<=n;i++)
    {
        cin>>c[i];
    }
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
        {
            memset(mx,-63,sizeof(mx));
            v.clear();
            dfs(i);
            /*for(auto i:v)
            {
                cout<<i<<' ';
            }
            cout<<endl;*/
            for(int i=1; i<=v.size(); i++)
            {
                for(int j=1; j<=v.size(); j++)
                {
                    int cnt=0;
                    if(j+i-1>v.size())
                    {
                        //cout<<j<<' '<<v.size()<<' '<<1<<' '<<i-(v.size()-j+1)<<endl;
                        for(int k=j; k<=v.size(); k++)
                        {
                            cnt+=v[k-1];
                        }
                        for(int k=1; k<=i-(v.size()-j+1); k++)
                        {
                            cnt+=v[k-1];
                        }
                    }
                    else
                    {
                        //cout<<j<<' '<<j+i-1<<endl;
                        for(int k=j; k<=j+i-1; k++)
                        {
                            cnt+=v[k-1];
                        }
                    }
                    mx[i]=max(mx[i],cnt);
                }
            }
            /*for(int i=1; i<=v.size(); i++)
            {
                cout<<mx[i]<<' ';
            }
            cout<<endl;*/
            for(int i=1; i<=v.size(); i++)
            {
                if(i<=k)
                {
                    ans=max(ans,mx[i]);
                }
                else
                {
                    /*int cnt=k/v.size();
                    (v.size()-(v.size()/k))
                    (k/v.size())*a[k]+a[(v.size()-(k/v.size()))]*/
                    /*cout<<k/v.size()<<' '<<k-(k/v.size())<<endl;
                    ans=max(ans,(k/v.size())*v[k]+v[(k-(k/v.size()))]);*/
                    break;
                }
            }
            //(k/v.size())*v[v.size()-1]
            //cout<<v[v.size()-1]<<' '<<v[(k-(k/v.size()))*v.size()-1]<<endl;
            ans=max(ans,(k/v.size())*mx[v.size()]+mx[(k-(k/v.size())*v.size())]);
        }
    }
    cout<<ans<<endl;
    return 0;
}
/*

*/


Compiler output:
a.cc: In function 'int main()':
a.cc:129:20: error: no matching function for call to 'max(ll&, long long unsigned int)'
  129 |             ans=max(ans,(k/v.size())*mx[v.size()]+mx[(k-(k/v.size())*v.size())]);
      |                 ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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:129:20: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'long long unsigned int')
  129 |             ans=max(ans,(k/v.size())*mx[v.size()]+mx[(k-(k/v.size())*v.size())]);
      |                 ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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:129:20: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  129 |             ans=max(ans,(k/v.size())*mx[v.size()]+mx[(k-(k/v.size())*v.size())]);
      |                 ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~