#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ for(auto it : P) { s << "<" << it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s << endl; }

const long long INF = 1LL<<60;
int N;
long long K;
vector<int> P;
vector<long long> C;

long long solve() {
    vector<bool> used(N, false);
    vector<vector<long long>> ss;
    for (int i = 0; i < N; ++i) {
        if (used[i]) continue;
        int cur = i;
        vector<long long> s;
        while (!used[cur]) {
            used[cur] = true;
            s.push_back(C[cur]);
            cur = P[cur];
        }
        ss.push_back(s);
    }

    //COUT(ss);

    long long res = -INF;
    for (int i = 0; i < N; ++i) chmax(res, C[i]);
    for (auto vec : ss) {
        long long M = vec.size();
        vector<long long> sum(M*2+1, 0);
        for (int i = 0; i < M*2; ++i) {
            sum[i+1] = sum[i] + vec[i%M];
        }

        long long q = K / M;
        long long r = K % M;
/*
        long long zen = -INF;
        for (int i = 0; i < M; ++i) {
            for (int j = 1; j <= min(M, K); ++j) {
                chmax(zen, sum[i+j] - sum[i]);
            }
        }
        chmax(res, zen);
        */

        vector<long long> amari(M, -INF);
        for (int i = 0; i < M; ++i) {
            for (int j = 0; j < M; ++j) {
                chmax(amari[j], sum[i+j] - sum[i]);
            }
        }
        for (int j = 0; j < M; ++j) {
            if (K < j) continue;
            long long q = (K - j) / M;
            if (q == 0 && j == 0) continue;
            if (j == 0) chmax(res, sum[M] + amari[j]);
            else chmax(res, amari[j]);
            chmax(res, sum[M] * q + amari[j]);
        }

/*
        long long zen = -INF, amari = -INF;
        for (int i = 0; i < M; ++i) {
            for (int j = 1; j <= min(M, K); ++j) {
                chmax(zen, sum[i+j] - sum[i]);
            }
            for (int j = 1; j <= r; ++j) {
                chmax(amari, sum[i+j] - sum[i]);
            }
        }
        chmax(res, zen);
        if (q) {
            chmax(res, q * sum[M]);
            chmax(res, q * sum[M] + amari);
        }
        */
    }
    return res;
}

int main() {
    while (cin >> N >> K) {
        P.resize(N);
        for (int i = 0; i < N; ++i) cin >> P[i], --P[i];
        C.resize(N);
        for (int i = 0; i < N; ++i) cin >> C[i];
        cout << solve() << endl;
    }
}

















Measured memory_bytes: 3852