#include <bits/stdc++.h>
#pragma GCC optimize(3, "Ofast")
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
inline int read() {
    int s = 0, w = 1;
    char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-')w = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
    return s * w;
}
#define f(i, l, r) for (int i = l; i <= r; ++i)
#define rf(i, r, l) for (int i = r; i >= l; --i)
#define all(x) (x).begin(), (x).end()
#define l l
#define r r
#define lch (o << 1)
#define rch (o << 1 | 1)
#define mid ((l + r) >> 1)
#define mem(x, y) memset(x, y, sizeof(x))
#define mod1(x) ((x >= mod) && (x -= mod))
#define mod2(x) ((x < 0) && (x += mod))

const int inf = 1e9 + 7;
const ll INF = INT64_MAX;
double eps = 1e-6;
const int mod = 1e9 + 7;
const int N = 1e6 + 10;
const double pi = acos(-1.0);
ll power(ll a, ll b) { ll res = 1; while (b) { if (b & 1)res = res * a % mod; a = a * a % mod; b >>= 1; }return res; }
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
void add(ll& a, ll b) { a += b; mod1(a); mod2(a); }
/*int len, C[N], clen;
int id(int v) {return lower_bound(C + 1, C + len, v) - C;}
void initC() { sort(C + 1, C +1+ clen); len = unique(C + 1, C + 1 + clen) - C - 1; }*/

vector<int> vt[N]; int tot;
ll p[N], c[N];
int vis[N];
ll score[N];

int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifdef local
    freopen("in.txt", "r", stdin);
    int start = clock();
#endif
    int n, k; cin >> n >> k;
    f(i, 1, n)cin >> p[i];
    f(i, 1, n)cin >> c[i];
    f(i, 1, n)if (!vis[i]) {
        int now = p[i];
        ++tot;
        while (now != i) {
            vis[now] = 1;
            score[tot] += c[now];
            vt[tot].push_back(now);
            now = p[now];
        }
        vis[i] = 1;
        score[tot] += c[i];
        vt[tot].push_back(i);
    }
    ll ans = -INF;
    f(i, 1, tot) {
        int w = vt[i].size();
        //for (auto it : vt[i])cout << c[it] << " "; cout << '\n';
        f(j, 0, w - 2)vt[i].push_back(vt[i][j]);
        
        if (score[i] <= 0) {
            f(len, 1, min(k, w)) {
                ll tmp = 0;
                int l = 0, r = len - 1;
                f(yy, l, r)tmp += c[vt[i][yy]];
                while (1) {
                    ans = max(ans, tmp);
                    if (l == w - 1)break;
                    tmp -= c[vt[i][l]];
                    tmp += c[vt[i][r + 1]];
                    l++; r++;
                }
            }
        }
        else {
            ll tmp2 = 0;
            ll t = k / w;
            if (t) {
                t--;
                tmp2 += t * score[i];
                k -= t * w;

                ll ans2 = 0;

                f(len, 1, w) {
                    ll tmp = 0;
                    int l = 0, r = len - 1;
                    f(yy, l, r)tmp += c[vt[i][yy]];
                    while (1) {
                        ans2 = max(ans2, tmp);
                        if (l == w - 1)break;
                        tmp -= c[vt[i][l]];
                        tmp += c[vt[i][r + 1]];
                        l++; r++;
                    }
                }

                ll ans3 = ans2;

                ans2 = 0;

                f(len, 1, k - w) {
                    ll tmp = 0;
                    int l = 0, r = len - 1;
                    f(yy, l, r)tmp += c[vt[i][yy]];
                    while (1) {
                        ans2 = max(ans2, tmp);
                        if (l == w - 1)break;
                        tmp -= c[vt[i][l]];
                        tmp += c[vt[i][r + 1]];
                        l++; r++;
                    }
                }
                ans3 = max(ans3, ans2 + score[i]);
                ans = max(ans, ans3 + tmp2);
                k += t * w;
            }
            else {
                ll ans2 = 0;

                f(len, 1, k) {
                    ll tmp = 0;
                    int l = 0, r = len - 1;
                    f(yy, l, r)tmp += c[vt[i][yy]];
                    while (1) {
                        ans2 = max(ans2, tmp);
                        if (l == w - 1)break;
                        tmp -= c[vt[i][l]];
                        tmp += c[vt[i][r + 1]];
                        l++; r++;
                    }
                }
                ans = max(ans, ans2);
            }

        }
    }
    cout << ans;
#ifdef local
    int end = clock();
    //cout << '\n' << end - start << "ms";
#endif* /
}


Measured memory_bytes: 27220