Language: C++

#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1000000007
using namespace std;


int main(){
  long n,k;
  cin >> n >> k;
  long p[n],c[n];
  rep(i,n){
    cin >> p[i];
    p[i]--;
  }
  rep(i,n)cin >> c[i];
  long maxsum = -INF, ans = -INF;
  long flag[n][n];
  rep(i,n)rep(j,n)flag[i][j] = 0;
  rep(i,n){
    long tmp_max = -INF, tmp = 0, now = i, iter = 0;
    while(1){
      if(flag[i][now]==1 || iter >= k)break;
      flag[i][now] = 1;
      now = p[now];
      tmp += c[now];
      if(tmp > tmp_max)tmp_max = tmp;
      iter++;
    }
    if(tmp_max > ans)ans = tmp_max;
    if(iter < k){
      long score = (k/iter)*tmp + (k%iter)*tmp_max;
      if(score > ans)ans = score;
    }
  }
  
  cout << ans << endl;
  return 0;
}



int main(){
  long n,k;
  cin >> n >> k;
  long p[n],c[n];
  rep(i,n)cin >> p[i];
  rep(i,n)cin >> c[i];
  long ans = 0, maxss = -INF;
  long flag[n][n];
  rep(i,n)rep(j,n)flag[i][j] = 0;
  long maxscore = -INF;
  int miman = 0;
  long tmp = 0;
  rep(i,n){
    long maxs = -INF;
    tmp = 0;
    long now = i;
    long iter = 0;
    while(1){
      if(flag[i][now]!=0)break;
      if(iter>=k){
        miman = 1;
        break;
      }
      tmp += c[p[now]];
      if(maxs<tmp)maxs = tmp;
      now = p[now];
      flag[i][now] = 1;
      iter++;
    }
    if(tmp>maxscore)maxscore = tmp;
    if(tmp>0 && maxs>maxss)maxss = maxs;
  }
  if(miman){
    cout << maxscore << endl;
    return 0;
  }
  cout << (k/n)*tmp + maxss << endl;
  return 0;
}



int main(){
  long n,k;
  cin >> n >> k;
  long p[n],c[n];
  rep(i,n)cin >> p[i];
  rep(i,n)cin >> c[i];
  vector<long,long> score[n];
  vector<long> scorenum;
  vector<long> lenscore;
  int flag[n];
  rep(i,n)flag[i] = 0;
  long i=0,iter=0,maxnum=0;
  while(1){
    if(flag[i]!=0){
      i++;
      continue;
    }
    long now=i,tmp=0,tmpscore=0;
    while(1){
      score[iter].push_back(now);
      flag[now] = 1;
      tmp++;
      tmpscore += p[now];
      if(flag[p[now]] != 0)break;
    }
    lenscore.push_back(tmp);
    scorenum.push_back(tmpscore);
    if(maxnum<tmp)maxnum = tmp;
    iter++;
  }
  if(maxnum>k)

  cout <<  << endl;
  return 0;
}

Compiler output:
a.cc:42:5: error: redefinition of 'int main()'
   42 | int main(){
      |     ^~~~
a.cc:7:5: note: 'int main()' previously defined here
    7 | int main(){
      |     ^~~~
a.cc:84:5: error: redefinition of 'int main()'
   84 | int main(){
      |     ^~~~
a.cc:7:5: note: 'int main()' previously defined here
    7 | int main(){
      |     ^~~~
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: In instantiation of 'struct std::_Vector_base<long int, long int>':
/usr/include/c++/14/bits/stl_vector.h:428:11:   required from 'class std::vector<long int, long int>'
  428 |     class vector : protected _Vector_base<_Tp, _Alloc>
      |           ^~~~~~
a.cc:90:28:   required from here
   90 |   vector<long,long> score[n];
      |                            ^
/usr/include/c++/14/bits/stl_vector.h:87:28: error: 'long int' is not a class, struct, or union type
   87 |         rebind<_Tp>::other _Tp_alloc_type;
      |                            ^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:89:9: error: 'long int' is not a class, struct, or union type
   89 |         pointer;
      |         ^~~~~~~
/usr/include/c++/14/bits/stl_vector.h: In instantiation of 'class std::vector<long int, long int>':
a.cc:90:28:   required from here
   90 |   vector<long,long> score[n];
      |                            ^
/usr/include/c++/14/bits/stl_vector.h:518:20: error: '_M_allocate' has not been declared in 'std::vector<long int, long int>::_Base'
  518 |       using _Base::_M_allocate;
      |                    ^~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:519:20: error: '_M_deallocate' has not been declared in 'std::vector<long int, long int>::_Base'
  519 |       using _Base::_M_deallocate;
      |                    ^~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:521:20: error: '_M_get_Tp_allocator' has not been declared in 'std::vector<long int, long int>::_Base'
  521 |       using _Base::_M_get_Tp_allocator;
      |                    ^~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/cassert:43,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:33:
/usr/include/c++/14/bits/stl_vector.h: In instantiation of 'std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_Vector_impl() [with _Tp = long int; _Alloc = long int]':
a.cc:90:28:   required from here
   90 |   vector<long,long> score[n];
      |                            ^
/usr/include/c++/14/bits/stl_vector.h:136:24: error: 'long int' is not a class, struct, or union type
  136 |         _Vector_impl() _GLIBCXX_NOEXCEPT_IF(
      |                        ^~~~~~~~~~~~~~~~~~~~
a.cc: In function 'int main()':
a.cc:116:12: error: expected primary-expression before '<<' token
  116 |   cout <<  << endl;
      |            ^~
/usr/include/c++/14/bits/stl_vector.h: In instantiation of 'std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_Vector_impl() [with _Tp = long int; _Alloc = long int]':
/usr/include/c++/14/bits/stl_vector.h:314:7:   required from here
  314 |       _Vector_base() = default;
      |       ^~~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:141:26: error: 'long int' is not a class, struct, or union type
  141 |         : _Tp_alloc_type()
      |                          ^