#include <bits/stdc++.h>
#define int int64_t
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define REPP(i, n) for (int i = 1; (i) < (int)(n+1); ++ (i))
#define REPR(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define FORR(i, m, n) for(int i=(m)-1; i>=(n); i--)
#define ALL(v) v.begin(), v.end()
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(),(c).end())
#define SORTR(c) sort((c).begin(),(c).end(),greater<int>());
#define BIT(n) (1LL<<(n))
#define PCNT(x) __builtin_popcountll(x)
#define GCD(x,y) __gcd(x,y)
#define LCM(x,y) x/__gcd(x,y)*y
#define PB push_back
#define MP make_pair
#define endl "\n"
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
using namespace std;
using Graph = vector<vector<int> >;
using WeightedGraph = vector<vector<pair<int, int> > >;
typedef long double LD;
typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
const int INF = 2000000000;
const long double PI = acosl(-1);
const int MOD = 1000000007;
void print() {cout << endl;}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head; if (sizeof...(tail) != 0) cout << " ";print(forward<Tail>(tail)...);}
template <class T> void print(vector<T> &vec) {for (auto& a : vec) {cout << a; if (&a != &vec.back()) cout << " ";} cout << endl;}
template <class T> void print(vector<vector<T>> &df) { for (auto& vec : df) { print(vec);}}
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }


signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    cout << fixed << setprecision(10);
    int ans = -INF*INF;
    
    int N, K;
    cin >> N;
    vector<int> P(N), C(N);
    cin >> K;
    REP (i, N) {
        cin >> P[i];
        P[i]--;
    }
    REP (i, N) {
        cin >> C[i];
    }

    vector<bool> look(N,false);
    VVI loop;
    REP(i,N) {
        if (look[i] == false) {
            int cur = i;
            vector<int> s;
            while (!look[cur]) {
                look[cur] = true;
                s.push_back(C[cur]);
                cur = P[cur];
            }
            loop.push_back(s);
        }
    }
    for (auto vec : loop) {
        int M = vec.size();
        VI sum(M*2+1, 0);
        REP(i,2*M) sum[i+1] = sum[i] + vec[i%M];

        vector<int> 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 r = 0; r < M; ++r) {
            if (r > K) continue;
            int q = (K - r) / M;
            if (r == 0 && q == 0) continue;

            if (sum[M] > 0) chmax(ans, amari[r] + sum[M] * q);
            else if (r > 0) chmax(ans, amari[r]);
        }
    }

    cout << ans << endl;

    
    
    
    
    
    return 0;
}

/*
cd ~/Desktop/Programming/AtCoder/practice/abc175/abc175_d && sh shellscripts/test_cpp.sh
cd ~/Desktop/Programming/AtCoder/practice/abc175/abc175_d && sh shellscripts/etest_cpp.sh
cd ~/Desktop/Programming/AtCoder/practice/abc175/abc175_d && sh shellscripts/submit_cpp.sh
*/


Measured memory_bytes: 3896