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

const long long mod = 1e9 + 7;

using ll = long long;
using pii  = pair<int, int>;
using pll = pair<ll, ll>;
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pii>
#define vpll vector<pll>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair

#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)

ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }


void show_vec(vll local_trip) {
    cout << "local_trip" << endl;
    rep(i, sz(local_trip)) {
        cout << local_trip[i] << ",";
    }
    cout << endl;
}

ll get_local_max(vll local_trip, ll local_k) {
    ll local_max = -INF;
    ll sum = 0;
    rep(i, local_k) {
        sum += local_trip[i];
        if (sum > local_max) {
            local_max = sum;
        }
    }
    return local_max;
}

ll get_sum(vll local_trip) {
    ll sum = 0;
    rep(i, sz(local_trip)) {
        sum += local_trip[i];
    }
    return sum;
}

ll get_local_max_score(vll local_trip, ll K) {
    // ll round_sum = accumulate(all(local_trip), 0);
    // ll round_sum = accumulate(local_trip.begin(), local_trip.end(), 0);
    ll round_sum = get_sum(local_trip);

    ll local_len = sz(local_trip);
    ll num_round, local_k;

    if (K % local_len == 0) {
        num_round = K / local_len - 1;
        local_k = local_len;
    } else {
        num_round = K / local_len;
        local_k = K % local_len;
    }

    ll local_max = get_local_max(local_trip, local_k);

    if (round_sum > 0 && num_round > 0) {
        local_max += (num_round * round_sum);
    }

    if (round_sum <= 0) {
        local_max = get_local_max(local_trip, local_len);
    }

    // show_vec(local_trip);
    // cout << "K = " << K << endl;
    // cout << "local_len = " << local_len << endl;
    // cout << "local_max = " << local_max << endl;
    // cout << "round_sum = " << round_sum << endl;
    // cout << "num_round = " << num_round << endl;
    // cout << "local_k = " << local_k << endl;

    return local_max;
}

void func(long long N, long long K, std::vector<long long> P, std::vector<long long> C){
    ll local_max;
    ll global_max = -INF;

    for (ll i = 0; i < N; i++){
        ll start_i = i;
        ll curr_i = i;
        vll local_trip;
        while (true) {
            curr_i = P[curr_i] - 1;
            local_trip.pb(C[curr_i]);
            if (start_i == curr_i) {
                break;
            }
        }
        local_max = get_local_max_score(local_trip, K);
        if (global_max < local_max) {
            global_max = local_max;
        }
    }
    cout << global_max << endl;
}

int main(){
    // cout << fixed << setprecision(5);

    long long N;
    scanf("%lld",&N);
    long long K;
    scanf("%lld",&K);
    std::vector<long long> P(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&P[i]);
    }
    std::vector<long long> C(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&C[i]);
    }
    func(N, K, std::move(P), std::move(C));
    return 0;
}


Measured memory_bytes: 3892