Language: C++

#include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0;i<(ll)n;i++)
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define spa << " " <<
#define fi first
#define se second
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()

using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long, long>;
using pdd = pair<ld, ld>;

template<typename T> struct V : vector<T> { using vector<T>::vector; };
V() -> V<ll>;
V(size_t) -> V<ll>;
template<typename T> V(size_t, T) -> V<T>;
template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }
template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;}
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;}
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
void fail() { cout << -1 << '\n'; exit(0); }
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<endl;}};
template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
for(ll i=1;i<n;i++)cerr spa v[i];
cerr<<endl;};

const ll INF = (1ll<<62);
// const ld EPS   = 1e-10;
// const ld PI    = acos(-1.0);
const ll mod = (int)1e9 + 7;
//const ll mod = 998244353;

int P[5000];
ll C[5000];
int Pd[30][5000];
ll Cd[30][5000];

int main(){

    int N, K;
    cin >> N >> K;
    // V P(N), C(N);
    REP(i, N) cin >> P[i], P[i]--;
    REP(i, N) cin >> C[i];

    // auto Pd = make_vec<ll>(30, N, 0ll);
    // auto Cd = make_vec<ll>(30, N, 0ll);

    REP(i, N) Pd[0][i] = P[i];
    REP(i, N) Cd[0][i] = C[P[i]];

    REP(i, 29){
        REP(j, N){
            Pd[i+1][j] = Pd[i][Pd[i][j]];
            Cd[i+1][j] = Cd[i][Pd[i][j]] + Cd[i][j];
        }
    }

    ll res = -INF;
    REP(i, N){
        for(int k=1;k<=min(N, K);k++){
            ll val = 0;
            int now = i;
            REP(i, 30){
            if(k>>i & 1){
                    val += Cd[i][now];
                    now = Pd[i][now];
                }
            }
            chmax(res, val);
        }
        for(int k=K-N;k<=K;k++){
            if(k<1) continue;
            ll val = 0;
            int now = i;
            REP(i, 30){
            if(k>>i & 1){
                    val += Cd[i][now];
                    now = Pd[i][now];
                }
            }
            chmax(res, val);
        }
    }
    cout << res << endl;

    return 0;
}#include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0;i<(ll)n;i++)
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define spa << " " <<
#define fi first
#define se second
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()

using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long, long>;
using pdd = pair<ld, ld>;

template<typename T> struct V : vector<T> { using vector<T>::vector; };
V() -> V<ll>;
V(size_t) -> V<ll>;
template<typename T> V(size_t, T) -> V<T>;
template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }
template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;}
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;}
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
void fail() { cout << -1 << '\n'; exit(0); }
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<endl;}};
template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
for(ll i=1;i<n;i++)cerr spa v[i];
cerr<<endl;};

const ll INF = (1ll<<62);
// const ld EPS   = 1e-10;
// const ld PI    = acos(-1.0);
const ll mod = (int)1e9 + 7;
//const ll mod = 998244353;

int P[5000];
ll C[5000];
int Pd[30][5000];
ll Cd[30][5000];

int main(){

    int N, K;
    cin >> N >> K;
    // V P(N), C(N);
    REP(i, N) cin >> P[i], P[i]--;
    REP(i, N) cin >> C[i];

    // auto Pd = make_vec<ll>(30, N, 0ll);
    // auto Cd = make_vec<ll>(30, N, 0ll);

    REP(i, N) Pd[0][i] = P[i];
    REP(i, N) Cd[0][i] = C[P[i]];

    REP(i, 29){
        REP(j, N){
            Pd[i+1][j] = Pd[i][Pd[i][j]];
            Cd[i+1][j] = Cd[i][Pd[i][j]] + Cd[i][j];
        }
    }

    ll res = -INF;
    REP(i, N){
        for(int k=1;k<=min(N, K);k++){
            ll val = 0;
            int now = i;
            REP(i, 30){
            if(k>>i & 1){
                    val += Cd[i][now];
                    now = Pd[i][now];
                }
            }
            chmax(res, val);
        }
        for(int k=K-N;k<=K;k++){
            if(k<1) continue;
            ll val = 0;
            int now = i;
            REP(i, 30){
            if(k>>i & 1){
                    val += Cd[i][now];
                    now = Pd[i][now];
                }
            }
            chmax(res, val);
        }
    }
    cout << res << endl;

    return 0;
}

Compiler output:
a.cc:102:2: error: stray '#' in program
  102 | }#include <bits/stdc++.h>
      |  ^
a.cc:102:3: error: 'include' does not name a type
  102 | }#include <bits/stdc++.h>
      |   ^~~~~~~
a.cc:120:29: error: redefinition of 'struct V<T>'
  120 | template<typename T> struct V : vector<T> { using vector<T>::vector; };
      |                             ^
a.cc:19:29: note: previous definition of 'struct V<T>'
   19 | template<typename T> struct V : vector<T> { using vector<T>::vector; };
      |                             ^
a.cc:121:1: error: redefinition of 'V()-> V<long long int>'
  121 | V() -> V<ll>;
      | ^
a.cc:20:1: note: 'V()-> V<long long int>' previously defined here
   20 | V() -> V<ll>;
      | ^
a.cc:122:1: error: redefinition of 'V(size_t)-> V<long long int>'
  122 | V(size_t) -> V<ll>;
      | ^
a.cc:21:1: note: 'V(size_t)-> V<long long int>' previously defined here
   21 | V(size_t) -> V<ll>;
      | ^
a.cc:123:22: error: redefinition of 'template<class T> V(size_t, T)-> V<T>'
  123 | template<typename T> V(size_t, T) -> V<T>;
      |                      ^
a.cc:22:22: note: 'template<class T> V(size_t, T)-> V<T>' previously declared here
   22 | template<typename T> V(size_t, T) -> V<T>;
      |                      ^
a.cc:124:32: error: redefinition of 'template<class T> std::vector<_Tp> make_vec(size_t, T)'
  124 | template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
      |                                ^~~~~~~~
a.cc:23:32: note: 'template<class T> std::vector<_Tp> make_vec(size_t, T)' previously declared here
   23 | template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
      |                                ^~~~~~~~
a.cc:125:31: error: redefinition of 'template<class ... Ts> auto make_vec(size_t, Ts ...)'
  125 | template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }
      |                               ^~~~~~~~
a.cc:24:31: note: 'template<class ... Ts> auto make_vec(size_t, Ts ...)' previously declared here
   24 | template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }
      |                               ^~~~~~~~
a.cc:126:37: error: redefinition of 'template<class S, class T> std::ostream& operator<<(std::ostream&, std::pair<_T1, _T2>)'
  126 | template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;}
      |                                     ^~~~~~~~
a.cc:25:37: note: 'template<class S, class T> std::ostream& operator<<(std::ostream&, std::pair<_T1, _T2>)' previously declared here
   25 | template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;}
      |                                     ^~~~~~~~
a.cc:127:31: error: redefinition of 'template<class T> std::ostream& operator<<(std::ostream&, const std::vector<_Tp>&)'
  127 | template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
      |                               ^~~~~~~~
a.cc:26:31: note: 'template<class T> std::ostream& operator<<(std::ostream&, const std::vector<_Tp>&)' previously declared here
   26 | template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
      |                               ^~~~~~~~
a.cc:128:28: error: redefinition of 'template<class T> std::ostream& operator<<(std::ostream&, const std::vector<std::vector<_Tp> >&)'
  128 | template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;}
      |                            ^~~~~~~~
a.cc:27:28: note: 'template<class T> std::ostream& operator<<(std::ostream&, const std::vector<std::vector<_Tp> >&)' previously declared here
   27 | template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;}
      |                            ^~~~~~~~
a.cc:129:8: error: redefinition of 'struct fast_ios'
  129 | struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
      |        ^~~~~~~~
a.cc:28:8: note: previous definition of 'struct fast_ios'
   28 | struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
      |        ^~~~~~~~
a.cc:129:118: error: conflicting declaration 'int fast_ios_'
  129 | struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
      |                                                                                                                      ^~~~~~~~~
a.cc:28:118: note: previous declaration as 'fast_ios fast_ios_'
   28 | struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
      |                                                                                                                      ^~~~~~~~~
a.cc:131:24: error: redefinition of 'template<class T> bool chmax(T&, const T&)'
  131 | template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
      |                        ^~~~~
a.cc:30:24: note: 'template<class T> bool chmax(T&, const T&)' previously declared here
   30 | template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
      |                        ^~~~~
a.cc:132:24: error: redefinition of 'template<class T> bool chmin(T&, const T&)'
  132 | template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
      |                        ^~~~~
a.cc:31:24: note: 'template<class T> bool chmin(T&, const T&)' previously declared here
   31 | template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
      |                        ^~~~~
a.cc:133:6: error: redefinition of 'void fail()'
  133 | void fail() { cout << -1 << '\n'; exit(0); }
      |      ^~~~
a.cc:32:6: note: 'void fail()' previously defined here
   32 | void fail() { cout << -1 << '\n'; exit(0); }
      |      ^~~~
a.cc:134:12: error: redefinition of 'int popcount(int)'
  134 | inline int popcount(const int x) { return __builtin_popcount(x); }
      |            ^~~~~~~~
a.cc:33:12: note: 'int popcount(int)' previously defined here
   33 | inline int popcount(const int x) { return __builtin_popcount(x); }
      |            ^~~~~~~~
a.cc:135:12: error: redefinition of 'int popcount(ll)'
  135 | inline int popcount(const ll x) { return __builtin_popcountll(x); }
      |            ^~~~~~~~
a.cc:34:12: note: 'int popcount(ll)' previously defined here
   34 | inline int popcount(const ll x) { return __builtin_popcountll(x); }
      |            ^~~~~~~~
a.cc:136:27: error: redefinition of 'template<class T> void debug(std::vector<std::vector<_Tp> >&, ll, ll)'
  136 | template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
      |                           ^~~~~
a.cc:35:27: note: 'template<class T> void debug(std::vector<std::vector<_Tp> >&, ll, ll)' previously declared here
   35 | template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
      |                           ^~~~~
a.cc:138:27: error: redefinition of 'template<class T> void debug(std::vector<_Tp>&, ll)'
  138 | template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
      |                           ^~~~~
a.cc:37:27: note: 'template<class T> void debug(std::vector<_Tp>&, ll)' previously declared here
   37 | template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
      |                           ^~~~~
a.cc:142:10: error: redefinition of 'const ll INF'
  142 | const ll INF = (1ll<<62);
      |          ^~~
a.cc:41:10: note: 'const ll INF' previously defined here
   41 | const ll INF = (1ll<<62);
      |          ^~~
a.cc:145:10: error: redefinition of 'const ll mod'
  145 | const ll mod = (int)1e9 + 7;
      |          ^~~
a.cc:44:10: note: 'const ll mod' previously defined here
   44 | const ll mod = (int)1e9 + 7;
      |          ^~~
a.cc:148:5: error: redefinition of 'int P [5000]'
  148 | int P[5000];
      |     ^
a.cc:47:5: note: 'int P [5000]' previously declared here
   47 | int P[5000];
      |     ^
a.cc:149:4: error: redefinition of 'll C [5000]'
  149 | ll C[5000];
      |    ^
a.cc:48:4: note: 'll C [5000]' previously declared here
   48 | ll C[5000];
      |    ^
a.cc:150:5: error: redefinition of 'int Pd [30][5000]'
  150 | int Pd[30][5000];
      |     ^~
a.cc:49:5: note: 'int Pd [30][5000]' previously declared here
   49 | int Pd[30][5000];
      |     ^~
a.cc:151:4: error: redefinition of 'll Cd [30][5000]'
  151 | ll Cd[30][5000];
      |    ^~
a.cc:50:4: note: 'll Cd [30][5000]' previously declared here
   50 | ll Cd[30][5000];
      |    ^~
a.cc:153:5: error: redefinition of 'int main()'
  153 | int main(){
      |     ^~~~
a.cc:52:5: note: 'int main()' previously defined here
   52 | int main(){
      |     ^~~~