Language: C++

#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <string>
#include <cstring>
#include <numeric>
#include <iomanip>
#include <fstream>
using namespace std;
long long mod=998244353;
int main(){
	int n,k;
	long long p[5000];
	long long c[5000];
	cin>>n>>k;
	for(int i=0;i<n;i++){
		cin>>p[i];
	}
	long long s=0;
	for(int i=0;i<n;i++){
		cin>>c[i];
		s+=c[i];
	}
	vector<int> a;
	int now=0;
	for(int i=0;i<n;i++){
		a.push_back(c[now]);
		now=p[now];
	}
	long long ans=0;
	if(s>0){
		ans+=k/n*s;
		k%=n;
	}
	for(int i=1;i<n;i++){
		a[i]+=a[i-1];
	}
	long long ma=0;
	for(int i=0;i<n;i++){
		for(int j=i+1;j<n;j++){
			if(j-i<=k){
				if(a[j]-a[i]>0){
					ma=max(ma,a[j]-a[i]);
				}
			}
		}
	}
	for(int i=0;i<n;i++){
		for(int j=i+1;j<n;j++){
			if(n-(j-i)<=k){
				if(s-(a[j]-a[i])>0){
					ma=max(ma,s-(a[j]-a[i]));
				}
			}
		}
	}
	cout<<ans+ma<<endl;
	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:49:47: error: no matching function for call to 'max(long long int&, __gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type)'
   49 |                                         ma=max(ma,a[j]-a[i]);
      |                                            ~~~^~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 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:49:47: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'})
   49 |                                         ma=max(ma,a[j]-a[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:49:47: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   49 |                                         ma=max(ma,a[j]-a[i]);
      |                                            ~~~^~~~~~~~~~~~~~