#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i,n) for(ll i = 0; i < (ll)(n); ++i)
#define FOR(i,a,b) for(ll i = (a); i < (ll)(b); ++i)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(v) ((int)v.size())
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b; return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b; return true;
    }
    return false;
}
template<int MOD> struct Fp {
    ll val;
    constexpr Fp(ll v = 0) noexcept : val(v % MOD) {
        if (val < 0) val += MOD;
    }
    constexpr int getmod() { return MOD; }
    constexpr Fp operator - () const noexcept {
        return val ? MOD - val : 0;
    }
    constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
    constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
    constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
    constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
    constexpr Fp& operator += (const Fp& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Fp& operator -= (const Fp& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Fp& operator *= (const Fp& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Fp& operator /= (const Fp& r) noexcept {
        ll a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            ll t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator == (const Fp& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator != (const Fp& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, ll n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};
constexpr ll MOD = 1e9 + 7;
using mint = Fp<MOD>;

#define pb push_back
#define mp make_pair
#define mt make_tuple

vector<ll> scores;

ll solve_slow(ll N, ll K, vector<ll>& Ps, vector<ll>& Cs) {
    ll ans = -1e18;
    REP(n, N) {
        ll pos = n;
        ll idx = 0;
        ll score = 0;
        REP(k, K) {
            ll nxt_pos = Ps[pos];
            ll nxt_score = Cs[nxt_pos];
            //cout << "pos, s = " << nxt_pos << "," << nxt_score << endl;
            score += nxt_score;
            chmax(ans, score);
            scores[idx] = nxt_score;
            ++idx;
            pos = nxt_pos;
        }
    }
    return ans;
}



ll solve_fast(ll N, ll K, vector<ll>& Ps, vector<ll>& Cs) {
    if (K <= N) return solve_slow(N, K, Ps, Cs);

    ll ans = -1e18;
    REP(n, N) {
        ll pos = n;
        ll idx = 0;
        ll score = 0;
        while (1) {
            ll nxt_pos = Ps[pos];
            ll nxt_score = Cs[nxt_pos];
            //cout << "pos, s = " << nxt_pos << "," << nxt_score << endl;
            score += nxt_score;
            chmax(ans, score);
            scores[idx] = nxt_score;
            ++idx;

            if (nxt_pos == n) {
                break;
            }
            pos = nxt_pos;
        }
        //REP(i, idx) cout << scores[i] << ",";
        //cout << endl;

        if (score > 0) {
            ll round = max(0LL, (K - N) / idx);
            ll tmp_score = round * score;
            chmax(ans, tmp_score);
            ll remain = K - round * idx;
            REP(j, remain) {
                tmp_score += scores[j % idx];
                chmax(ans, tmp_score);
            }
        }
    }
    return ans;
}

int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    scores.resize(5010);

    //while (1) {
    //    ll N = 4;
    //    ll K = rand() % 100 + 1;
    //    vector<ll> Ps(N);
    //    REP(n, N) Ps[n] = n;
    //    random_shuffle(ALL(Ps));
    //    vector<ll> Cs(N);
    //    REP(n, N) Cs[n] = rand() % 10 - 5;

    //    auto gt = solve_slow(N, K, Ps, Cs);
    //    auto mine = solve_fast(N, K, Ps, Cs);
    //    if (gt != mine) {
    //        auto mine2 = solve_fast(N, K, Ps, Cs);
    //    }
    //    else {
    //        cout << "ok" << endl;
    //    }


    //}


    ll N, K;
    cin >> N >> K;
    vector<ll> Ps(N);
    vector<ll> Cs(N);
    REP(n, N) {cin >> Ps[n]; Ps[n]--;}
    REP(n, N) cin >> Cs[n];
    cout << solve_fast(N, K, Ps, Cs) << endl;
    
    return 0;
}


Measured memory_bytes: 3776