Language: C++

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1e9
#define PI 3.14159265359
#define MOD 1000000007
#define ALL(v) v.begin(),v.end()
#define ALLR(v) v.rbegin(),v.rend()
#define F first
#define S second
typedef long long ll;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
//isPrime
//modpow modinv
//getDigit
template<class T> inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template<class T> inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  cout << fixed << setprecision(10);
  int n,k;
  cin >> n >> k;
  vector<ll> p(n),c(n);
  rep(i,n) cin >> p[i];
  rep(i,n) p[i]--;
  rep(i,n) cin >> c[i];
  vector<vector<int>> a;
  map<int,int> mp;
  rep(i,n){
    if(mp[i]!=0)continue;
    mp[i]++;
    int q = i;
    vector<int> b;
    rep(j,n){
      b.push_back(c[q]);
      q=p[q];
      if(mp[q]!=0){
        break;
      }
      mp[q]++;
    }
    a.push_back(b);
  }
  ll t = -1e18;
  for(auto b:a){
    int m = b.size();
    vector<ll> d(2*m+1);
    rep(i,m){
      d[i+1]=d[i]+b[i];
    }
    rep(i,m){
      d[i+m+1]=d[i+m]+b[i];
    }
    ll res = 0;
    if(k%m==0&&k>=m){
      res+=(k/m-1)*(d[m]-d[0]);
    }else{
      res += (k/m)*(d[m]-d[0]);
    }
    res = max(res,0);
    int k1;
    if(k%m==0&&k>=m){
      k1=m;
    }else{
      k1=k%m;
    }
    ll ans = -1e18;
    for(int add=1; add<=k1; add++){
      for(int i=0; i+add<2*m+1; i++){
        ans = max(ans,d[i+add]-d[i]);
      }
    }
    t = max(t,ans+res);
  }
  cout << t << endl;

}


Compiler output:
a.cc: In function 'int main()':
a.cc:72:14: error: no matching function for call to 'max(ll&, int)'
   72 |     res = max(res,0);
      |           ~~~^~~~~~~
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:72:14: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   72 |     res = max(res,0);
      |           ~~~^~~~~~~
/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:72:14: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   72 |     res = max(res,0);
      |           ~~~^~~~~~~