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

#define rep(i,x,y) for(ll i=(x);i<(y);i++)
#define rrep(i,x,y) for(ll i=(ll)(y)-1;i>=(x);i--)
#define all(x) (x).begin(),(x).end()
#define itrout(x) for(int i=0;i<x.size();i++) {cout << x[i] << (i==x.size()-1 ? "\n" : " ");}
#ifdef LOCAL
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl
#define debugbit(x, n) cerr << #x << " = " << bitset<n>(x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl
#define itrdebug(x) cerr << #x << " "; for (auto & el : (x)) {cerr << (el) << " ";} cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define debugbit(x, n)
#define itrdebug(x)
#define dassert(...)
#endif
//#define int long long

typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;

void solve(long long N, long long K, std::vector<long long> P, std::vector<long long> C){
    ll answer = LLONG_MIN;
    rep(start, 1, N+1) {
        ll score = 0;
        if (K <= N) {
            int current = start;
            rep(i,0,K) {
                current = P[current];
                score += C[current];
                answer = max(answer, score);
            }
        } else {
            int current = start, k = K;
            int loopStart = -1, loopCnt = 0, loopStartOffset = 0;
            ll loopScore = 0;
            vector<int> distance(N+1, -1);
            distance[start] = 0;
            // get loopStart, loopStartOffset
            rep(i,0,K) {
                current = P[current];
                if (distance[current] != -1) {
                    loopStart = current;
                    loopStartOffset = distance[loopStart];
                    break;
                }
                distance[current] = i+1;
            }
            // get loopCnt, loopScore
            rep(i,0,K) {
                current = P[current];
                loopCnt++;
                loopScore += C[current];
                if (current == loopStart) break;
            }
            // calculate score before loop
            rep(i,0,loopStartOffset) {
                current = P[current];
                score += C[current];
                answer = max(answer, score);
            }
            k -= loopStartOffset;
            // calculate score during loop
            if (loopScore > 0) { // do loop
                // leave last one loop because there is possibility that score is maximized during loop
                score += max((k / loopCnt)-1, 0) * loopScore;
                k -= max((k / loopCnt)-1, 0) * loopCnt;
            } else { // do not loop
                k = min(k, loopCnt);
            }
            // calculate score after loop
            current = loopStart;
            rep(i,0,k) {
                current = P[current];
                score += C[current];
                answer = max(answer, score);
            }
        }
    }
    cout << answer << endl;
}

signed main(){
    // ios_base::sync_with_stdio(false);
    // cin.tie(NULL);
    long long N;
    scanf("%lld",&N);
    long long K;
    scanf("%lld",&K);
    std::vector<long long> P(N+1);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&P[i+1]);
    }
    std::vector<long long> C(N+1);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&C[i+1]);
    }
    solve(N, K, std::move(P), std::move(C));
    return 0;
}


Measured memory_bytes: 3772