#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

const ll INF = 1e18;

int main() {
    int n, k;
    cin >> n >> k;
    vector<int> p(n + 1), c(n + 1);
    for (int i = 1; i <= n; i++) cin >> p[i];
    for (int i = 1; i <= n; i++) cin >> c[i];
    ll res = *max_element(c.begin() + 1, c.end());
    for (int i = 1; i <= n; i++) {
        ll j = i, sum = 0, len = 0;
        do {
            sum += c[j], j = p[j], len++;
        } while (j != i);
        ll r = k >= len ? len + k % len : k;
        ll base = ((k - r) / len) * max(sum, 0ll), add = -INF;
        sum = 0;
        for (int t = 1; t <= r; t++) {
            sum += c[j], j = p[j];
            add = max(add, sum);
        }
        res = max(res, base + add);
    }
    cout << res << "\n";
}
