Language: C++

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <string>

#define rep(i, a, b) for ( int i = (a); i < (b); i++ )
#define per(i, a, b) for ( int i = (b)-1; i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sz(v) (int)(v).size()

using namespace std;

typedef long long ll;
typedef long double ld;

using P = pair<int, int>;
template<typename T>
struct Edge {
    int u, v;
    T cost;
    Edge(int u, int v, T c) : u(u), v(v), cost(c) {}
    bool operator< (const Edge &e) const {return cost < e.cost;}
};

static const long long MOD = 1000000007;
static const long long LINF = (ll)(1e18+99);
static const int INF = 1e9+99;

ll ans = -LINF;

int main(void) {
  int N;
  int K;
  cin >> N >> K;
  vector<int> P(N, 0);
  vector<ll> C(N, 0);
  rep(i, 0, N) {
    cin >> P[i];
    P[i]--;
  }
  rep(i, 0, N) cin >> C[i];

  rep(i, 0, N) {
    int size = 0;
    ll sum = 0;
    int pos = i;
    do {
      sum += C[P[pos]];
      pos = P[pos];
      size++;
    } while (pos != i);

    if (sum<=0) {
      int x = P[1000000];
      int p = i;
      ll x = 0;
      rep(j, 0, min(size,K)) {
        x += C[P[p]];
        p = P[p];
        ans = max(ans, x);
      }

    } else {
      int q = K/size, r = K%size;
      sum = sum*q;
      if (q!=0) ans = max(ans, sum);
      pos = i;
      rep(j, 0, r) {
        sum += C[P[pos]];
        pos = P[pos];
        ans = max(ans, sum);
      }
    }
  }
    
  
    
  cout << ans << endl;
  return 0;
}





Compiler output:
a.cc: In function 'int main()':
a.cc:66:10: error: conflicting declaration 'll x'
   66 |       ll x = 0;
      |          ^
a.cc:64:11: note: previous declaration as 'int x'
   64 |       int x = P[1000000];
      |           ^
a.cc:70:18: error: no matching function for call to 'max(ll&, int&)'
   70 |         ans = max(ans, x);
      |               ~~~^~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 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:70:18: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   70 |         ans = max(ans, x);
      |               ~~~^~~~~~~~
/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,
                 from a.cc:2:
/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:70:18: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   70 |         ans = max(ans, x);
      |               ~~~^~~~~~~~