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

#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define endl '\n'

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;

const ll mod = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// head

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, k;
    cin >> n >> k;
    vi p(n + 1);
    vi c(n + 1);
    rep(i,1,n + 1) cin >> p[i];
    rep(i,1,n + 1) cin >> c[i];

    ll ans = LLONG_MIN;
    rep(i,1,n + 1) { // iterate over start
        vector<bool> vis(n + 1); // visited elements
        vector<int> cyc;
        vector<int> cycleElements;
        int moves = k;
        int curr = i;
        ll p1 = 0;
        ll p2 = 0;
        ll a = LLONG_MIN;
        while(!vis[curr] && moves > 0) {
            vis[curr] = true;
            cyc.pb(curr);
            curr = p[curr];
            moves--;
        }
        if(vis[curr]) {
            while(sz(cyc) > 0 && cyc.back() != curr) {
                cycleElements.pb(cyc.back());
                p2 += c[cyc.back()];
                cyc.pop_back();
            }
            if(sz(cyc) > 0) {
                cycleElements.pb(curr);
                p2 += c[cyc.back()];
                cyc.pop_back();
            }
        }
        reverse(all(cycleElements));
        //trav(y, cycleElements) cout << y << " ";
        //cout << endl;
        ll sum = 0;
        trav(x, cyc) {
            sum += c[x];
            a = max(a, sum);
        }
        ll x = 0;
        moves = k - sz(cyc);
        for(int j = 0; j < min(sz(cycleElements), moves); j++) { // check if some prefix of the cycle elements is > a (because may not be optimal to go through whole cycle)
            x += c[cycleElements[j]];
            a = max(a, x + sum);
        }
        if(p2 > 0) { // otherwise it is not beneficial to continue to go in the cycle.... (entirely)
            // idea might be to run the loop until you can only run through it once more, then check the prefix max at that point
            if(sz(cycleElements) > 0) {
                int rev = moves / sz(cycleElements);
                if(rev > 0) {
                    sum += p2 * (rev - 1);
                    a = max(a, sum);
                    for(int j = 0; j < sz(cycleElements); j++) {
                        sum += c[cycleElements[j]];
                        a = max(a, sum);
                    }
                }
                else sum += p2 * (moves / sz(cycleElements));
                a = max(a, sum);
                moves %= sz(cycleElements);
                for(int j = 0; j < moves; j++) {
                    sum += c[cycleElements[j]];
                    a = max(a, sum);
                }
            }
        } /*else {
            for(int j = 0; j < min(sz(cycleElements), moves); j++) {
                sum += c[cycleElements[j]];
                a = max(sum, a);
            }
        }*/
        
        ans = max(ans, a);
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3776