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

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

class Prints {
private:
    class __Prints {
    public:
        __Prints(const char* sep, const char* term) : sep(sep), term(term) {}
        template <class... Args>
        void operator()(Args&&... args) const { print(std::forward<Args>(args)...); }

    private:
        const char *sep, *term;
        void print() const { std::cout << term; }
        void print_rest() const { std::cout << term; }
        template <class T, class... Tail>
        void print(T&& head, Tail&&... tail) const { std::cout << head, print_rest(std::forward<Tail>(tail)...); }
        template <class T, class... Tail>
        void print_rest(T&& head, Tail&&... tail) const { std::cout << sep << head, print_rest(std::forward<Tail>(tail)...); }
    };

public:
    Prints() {}
    __Prints operator()(const char* sep = " ", const char* term = "\n") const { return __Prints(sep, term); }
    template <class T>
    void print_vec(const std::vector<T>& vec, const char* sep = " ", const char* term = "\n") const {
        const size_t sz = vec.size();
        for (size_t i = 0; i < sz; i++) std::cout << vec[i] << (i == sz - 1 ? term : sep);
    }
};

Prints prints;

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void solve() {
    ll N, K;
    cin >> N >> K;
    Vl P(N), C(N);
    for (ll i = 0; i < N; i++) {
        cin >> P[i];
        --P[i];
    }
    for (ll i = 0; i < N; i++) {
        cin >> C[i];
    }
    vector<Vl> roops;
    Vl ts;
    vector<bool> used(N);
    for (ll i = 0; i < N; i++) {
        if (used[i]) continue;
        ts.clear();
        ll idx = i;
        while (!used[idx]) {
            ts.emplace_back(C[idx]);
            used[idx] = true;
            idx = P[idx];
        }
        roops.emplace_back(Vl());
        Vl& target = roops.back();
        target.insert(target.end(), ts.begin(), ts.end());
        target.insert(target.end(), ts.begin(), ts.end());
    }
    ll ans = -L_INF;
    Vl cum;
    for (const auto& vec : roops) {
        ll n = (ll)vec.size() / 2;
        cum.assign(n * 2 + 1, 0);
        for (ll i = 0; i < n * 2; i++) {
            cum[i + 1] = cum[i] + vec[i];
        }
        ll mx = -L_INF;
        ll b = K % n;
        ll c = K / n;
        if (b == 0) {
            b = n;
            --c;
        }

        for (ll i = 0; i < n; i++) {
            ll t = -L_INF;
            for (ll j = i + 1; j <= i + b; j++) {
                t = max(t, cum[j] - cum[i]);
            }
            mx = max(mx, t);
        }
        if (cum[n] > 0) {
            mx += c * cum[n];
        }
        ans = max(ans, mx);

        if (b != n) {
            mx = -L_INF;
            for (ll i = 0; i < n; i++) {
                ll t = -L_INF;
                for (ll j = i + b + 1; j <= i + n; j++) {
                    t = max(t, cum[j] - cum[i]);
                }
                mx = max(mx, t);
            }
            if (cum[n] > 0) {
                mx += (c - 1) * cum[n];
            }
            ans = max(ans, mx);
        }
    }
    prints()(ans);
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
    cin.tie(0), cout.tie(0);
    ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
    ifstream ifs("./in_out/input.txt");
    cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
    ofstream ofs("./in_out/output.txt");
    cout.rdbuf(ofs.rdbuf());
#endif
    cout << setprecision(18) << fixed;
    solve();
    cout << flush;
    return 0;
}

Measured memory_bytes: 3828