#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <stack>
#include <functional>

#ifdef LOCAL
    #define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
    #define eprintf(...)
#endif

#define rep_(i, a_, b_, a, b, ...) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...) for (int i = (b-1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fls(x) (64 - __builtin_clzll(x))
#define pcnt(x) __builtin_popcountll(x)
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair <int,int> P;
typedef long double ld;

int main (void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, k; cin >> n >> k;
    vector<ll> p(n), c(n);
    rep (i, n) { cin >> p[i]; p[i]--; }
    rep (i, n) cin >> c[i];
    if (n + 10 >= k) {
        ll ans = (ll)-1e18;
        rep (i, n) {
            ll now = i, score = 0;
            rep (s, k) {
                now = p[now];
                score += c[now];
                chmax(ans, score);
            }
        }
        cout << ans << "\n";
    } else {
        ll ans = (ll)-2e18;
        rep (i, n) {
            vector<bool> used(n);
            int now = i, loop_start, length_bef_loop = 0;
            ll max_bef_loop = (ll)-2e18, score_bef_loop = (ll)0;
            while (used[now] == false) {
                used[now] = true;
                now = p[now];
                score_bef_loop += c[now];
                chmax(max_bef_loop, score_bef_loop);
                length_bef_loop ++;
            }
            loop_start = now;
            // eprintf("%d ", loop_start);
            int length = 0;
            ll max_in_loop = (ll)-2e18, argmax_in_loop = -1, score_per_loop = (ll)0;
            while (length == 0 || now != loop_start) {
                now = p[now];
                length++;
                score_per_loop += c[now];
                if (chmax(max_in_loop, score_per_loop)) argmax_in_loop = length;
            }

            ll ani = max(max_bef_loop, score_bef_loop + max_in_loop);
            int rest = max((k - length_bef_loop) / length - 1, 0LL);
            ll tmp = score_bef_loop + rest * score_per_loop;
            chmax(ani, tmp);
            int noi = loop_start;
            rep (_, k - (length_bef_loop + length * rest)) {
                noi = p[noi];
                tmp += c[noi];
                chmax(ani, tmp);
            }
            eprintf("%d -> %d, %d %lld %lld / %lld %lld %lld : %lld\n",
                i, loop_start, length, max_bef_loop, score_bef_loop, max_in_loop, argmax_in_loop, score_per_loop, ani);
            chmax(ans, ani);
            
        }
        cout << ans << "\n";
    }
    return 0;
}

Measured memory_bytes: 3732