#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 DEBUG
#ifdef DEBUG
void debug() { cerr << "\n"; }
template <class T> void debug(const T &x) { cerr << x << "\n"; }
template <class T, class... Args> void debug(const T &x, const Args &... args) {
    cerr << x << " ";
    debug(args...);
}
template <class T> void debugVector(const vector<T> &v) {
    for(const T &x : v) {
        cerr << x << " ";
    }
    cerr << "\n";
}
#else
template <class T, class... Args>
void debug(const T &x, const Args &... args) {}
template <class T> void debugVector(const vector<T> &v) {}
#endif

using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, K;
    cin >> N >> K;
    vector<int> p(N);
    vector<ll> c(N);
    for(int i = 0; i < N; i++) {
        cin >> p[i];
        p[i]--;
    }
    for(int i = 0; i < N; i++) {
        cin >> c[i];
    }
    ll ans = -LLINF;
    for(int s = 0; s < N; s++) {
        vector<bool> seen(N, false);
        vector<int> cycle;
        cycle.emplace_back(s);
        seen[s] = true;
        int now = p[s];
        while(!seen[now]) {
            cycle.emplace_back(now);
            seen[now] = true;
            now = p[now];
        }
        debug("-------");
        debug(s);
        int loopSize = cycle.size();
        ll loopSum = 0;
        for(int i : cycle) {
            loopSum += c[i];
        }
        debug("loopSum", loopSum);
        int cnt = K;
        ll res = 0;
        cycle.emplace_back(s);
        cycle.erase(cycle.begin());
        debugVector(cycle);
        if(cnt <= loopSize) {
            res = c[cycle[0]];
            ll cur = 0;
            for(int i = 0; i < cnt; i++) {
                cur += c[cycle[i]];
                chmax(res, cur);
            }
            chmax(ans, res);
            debug("res", res);
            continue;
        }
        if(loopSum > 0) {
            if(cnt / loopSize > 0) {
                ll tmp = cnt / loopSize - 1;
                ll rec = loopSum * tmp;
                ll mx = 0;
                if(tmp == 0) {
                    mx = c[cycle[0]];
                }
                ll cur = 0;
                for(int i = 0; i < loopSize; i++) {
                    cur += c[cycle[i]];
                    chmax(mx, cur);
                }
                rec += mx;
                chmax(ans, rec);
            }
            res += loopSum * (cnt / loopSize);
            cnt = cnt % loopSize;
            ll mx = 0;
            ll cur = 0;
            for(int i = 0; i < cnt; i++) {
                cur += c[cycle[i]];
                chmax(mx, cur);
            }
            res += mx;
            chmax(ans, res);
        } else {
            ll mx = c[cycle[0]];
            ll cur = 0;
            for(auto i : cycle) {
                cur += c[i];
                chmax(mx, cur);
            }
            res += mx;
            chmax(ans, res);
        }
        debug("res", res);
    }
    cout << ans << "\n";
}

Measured memory_bytes: 3768