Language: C++

#include<bits/stdc++.h>
using namespace std;
long long inf=10000000000;

int main(){
	int n,k;
	cin>>n>>k;
	vector<int> p(n);
	for(int i=0;i<n;i++){
		cin>>p[i];
		p[i]--;
	}
	vector<int> c(n);
	for(int i=0;i<n;i++){
		cin>>c[i];
	}
	if(k<=n){
		long long ans=-inf;
		for(int i=0;i<n;i++){
			long long P=i;
			long long s=0;
			for(int j=0;j<k;++j){
				P=p[P];
				s+=c[P];
				ans=max(ans,s);
			}
		}
		cout<<ans<<endl;
	}else{
		vector<vector<int> > next(31,vector<int>(n));
		for(int i=0;i<n;i++){
			next[0][i]=p[i];
		}
		for(int i=1;i<31;i++){
			for(int j=0;j<n;j++){
				next[i][j]=next[i-1][next[i-1][j]];
			}
		}
		vector<vector<long long> >sum(31,vector<long long>(N));
		for(int i=0;i<N;i++){
			sum[0][i]=c[p[i]];
		} 
		for(int i=1;i<31;i++){
			for(int j=0;j<n;j++){
				sum[i][j]=sum[i-1][j]+sum[i-1][next[i-1][j]];
			}
		}
		long long ans=0;
		for(int i=0;i<n;i++){
			long long P=i;
			long long s=0;
			for(int j=0;j<n;j++){
				P=p[P];
				s+=c[P];
				ans=max(ans,s);
			}
			P=i;
			s=0;
			for(int j=0;j<31;j++){
				if((k-n)>>j&1){
					s+=sum[j][p];
					p=next[j][p];
				}
			}
			ans=max(ans,s);
			for(int j=0;j<n;j++){
				P=p[P];
				s+=c[P];
				ans=max(ans,s);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:39:68: error: 'N' was not declared in this scope
   39 |                 vector<vector<long long> >sum(31,vector<long long>(N));
      |                                                                    ^
a.cc:61:50: error: no match for 'operator[]' (operand types are '__gnu_cxx::__alloc_traits<std::allocator<std::vector<long long int> >, std::vector<long long int> >::value_type' {aka 'std::vector<long long int>'} and 'std::vector<int>')
   61 |                                         s+=sum[j][p];
      |                                                  ^
In file included from /usr/include/c++/14/vector:66,
                 from /usr/include/c++/14/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_vector.h:1128:7: note: candidate: 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](size_type) [with _Tp = long long int; _Alloc = std::allocator<long long int>; reference = long long int&; size_type = long unsigned int]'
 1128 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1128:28: note:   no known conversion for argument 1 from 'std::vector<int>' to 'std::vector<long long int>::size_type' {aka 'long unsigned int'}
 1128 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |                  ~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_vector.h:1147:7: note: candidate: 'std::vector<_Tp, _Alloc>::const_reference std::vector<_Tp, _Alloc>::operator[](size_type) const [with _Tp = long long int; _Alloc = std::allocator<long long int>; const_reference = const long long int&; size_type = long unsigned int]'
 1147 |       operator[](size_type __n) const _GLIBCXX_NOEXCEPT
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1147:28: note:   no known conversion for argument 1 from 'std::vector<int>' to 'std::vector<long long int>::size_type' {aka 'long unsigned int'}
 1147 |       operator[](size_type __n) const _GLIBCXX_NOEXCEPT
      |                  ~~~~~~~~~~^~~
a.cc:62:50: error: no match for 'operator[]' (operand types are '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} and 'std::vector<int>')
   62 |                                         p=next[j][p];
      |                                                  ^
/usr/include/c++/14/bits/stl_vector.h:1128:7: note: candidate: 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](size_type) [with _Tp = int; _Alloc = std::allocator<int>; reference = int&; size_type = long unsigned int]'
 1128 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1128:28: note:   no known conversion for argument 1 from 'std::vector<int>' to 'std::vector<int>::size_type' {aka 'long unsigned int'}
 1128 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |                  ~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_vector.h:1147:7: note: candidate: 'std::vector<_Tp, _Alloc>::const_reference std::vector<_Tp, _Alloc>::operator[](size_type) const [with _Tp = int; _Alloc = std::allocator<int>; const_reference = const int&; size_type = long unsigned int]'
 1147 |       operator[](size_type __n) const _GLIBCXX_NOEXCEPT
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1147:28: note:   no known conversion for argument 1 from 'std::vector<int>' to 'std::vector<int>::size_type' {aka 'long unsigned int'}
 1147 |       operator[](size_type __n) const _GLIBCXX_NOEXCEPT
      |                  ~~~~~~~~~~^~~