Language: C++

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>  // sort, 二分探索, etc
#include <iomanip>  // setprecision (浮動小数点の出力の誤差)
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <cmath>
#include <list>
#include <numeric>

#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=a,i##_len=(b);i<i##_len;++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define cauto const auto&
#define UNIQUE(v) (v).erase( unique(all(v), (v).end() );
#define SZ(x) ((ll)(x).size()) // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(all(x))
#define MIN(x) *min_element(all(x))
#define SUM(v) accumulate((v).begin(), (v).end(), (ll)0)
#define INF 1000000000000
#define MOD 1000000007
#define perm(c) sort(all(c));for(bool c##p=1;c##p;c##p=next_permutation(all(c)))

using namespace std;
using ll = long long;
using ull = unsigned long long;

template<class T>bool chmax(T &a, const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a, const T &b){if(a>b){a=b;return true;}return false;}

const char newl = '\n';

ll qp(ll a,ull b){ll ans=1;do{if(b&1u)ans*=a;a*=a;}while(b>>=1u);return ans;}
ll qp(ll a,ull b,ll mod){ll ans=1;do{if(b&1u)ans=ans*a%mod;a=a*a%mod;}while(b>>=1u);return ans;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll comb(int n, int r) {
    vector<vector<ll> > v(n+1, vector<ll>(r+1, 0));
    for(int i=0;i<n+1;i++) {v[i][0]=1; if(i < r+1)v[i][i]=1; }
    for(int i=1;i<n+1;i++) {for(int j=1;j<min(i,r+1);j++) {v[i][j]=v[i-1][j-1]+v[i-1][j];}}
    return v[n][r];
}

// INT_MAX   = 2.1e+09 (2147483647)
// LONG_MAX  = 9.2e+18 (9223372036854775807)
// LLONG_MAX = 9.2e+18 (9223372036854775807)


ll loop_max(vector<ll> vec, ll mov_max) {
    assert(mov_max > 0);  // 0個以上の点を選べる
    ll len = vec.size();
    assert(len > 0);
    ll max_score = -INF;
    vector<vector<ll> > mat(len, vector<ll>(len, 0));
    rep(d, len) {
        if (d >= mov_max) {
            break;
        }
        rep(start, len) {
            ll val;
            if (d == 0) {
                val = vec[start];
            } else {
                val = mat[start][start] + mat[(start + 1) % len][(start + d) % len];
            }
            max_score = max(val, max_score);
            mat[start][(start + d)%len] = val;
        }
    }
    return max_score;
}

ll solver(vector<ll> vec, ll k) {
    ll len = vec.size();
    assert(len > 0);
    ll sum = SUM(vec);
    if (k <= len) {
        return loop_max(vec, min(k, len));
    }
    if (sum > 0) {
        ll offset = (k / len - 1) * sum;
        ll lhs;
        if (k % len == 0) {
            lhs = sum;
        } else {
            lhs = sum + loop_max(vec, k % len);
        }
        offset += max(lhs, loop_max(vec, len));
        return offset;
    } else {
        return loop_max(vec, len);
    }
//    if (sum > 0) {
//        ll offset = (k / len) * sum;
//        k = (k % len);
//        if (k == 0) {
//            return offset;
//        } else {
//            return offset + loop_max(vec, k);
//        }
//    } else {
//        return loop_max(vec, min(k, len));
//    }
}


int main() {
    ll n; cin >> n;
    ll k; cin >> k;
    vector<ll> ps;
    vector<ll> cs;
    rep(i, n) {
        ll x; cin >> x;
        ps.push_back(x - 1);
    }
    rep(i, n) {
        ll x; cin >> x;
        cs.push_back(x);
    }

    map<ll, ll> mp;
    vector<vector<ll> > vvec;
    rep(i, n) {
        if (mp[i] != 0) {
            continue;
        }
        vector<ll> vec;
        vec.push_back(cs[i]);
        mp[i] += 1;
        ll idx = ps[i];
        while (idx != i) {
            vec.push_back(cs[idx]);
            mp[idx] += 1;
            idx = ps[idx];
        }
        vvec.push_back(vec);
    }

    ll mmax_score = -INF;
    assert(k != 0);
    for (const auto &vec : vvec) {
        ll max_score = solver(vec, k);
        mmax_score = max(max_score, mmax_score);
    }

//    for (const auto &vec : vvec) {
//        for (const auto &val : vec) {
//            cout << val << ' ';
//        }
//        cout << newl;
//    }

    assert(mmax_score != -INF);
    cout << mmax_score << newl;


    return 0;
}


Compiler output:
a.cc: In function 'll loop_max(std::vector<long long int>, ll)':
a.cc:54:5: error: 'assert' was not declared in this scope
   54 |     assert(mov_max > 0);  // 0個以上の点を選べる
      |     ^~~~~~
a.cc:13:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
   12 | #include <numeric>
  +++ |+#include <cassert>
   13 | 
a.cc: In function 'll solver(std::vector<long long int>, ll)':
a.cc:79:5: error: 'assert' was not declared in this scope
   79 |     assert(len > 0);
      |     ^~~~~~
a.cc:79:5: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
a.cc: In function 'int main()':
a.cc:144:5: error: 'assert' was not declared in this scope
  144 |     assert(k != 0);
      |     ^~~~~~
a.cc:144:5: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'