#include <bits/stdc++.h>

#ifdef DEBUG
#define PRINT(x)\
    cout<<"func "<<__func__<<": line "<<__LINE__<<": "<<#x<<" = "<<(x)<<endl;
#define PRINTA(a,first,last)\
    cout<<"func "<<__func__<<": line "<<__LINE__<<": "<<#a<<"["<<(first)<<", "<<(last)<<")"<<endl;\
    for (int _i=(first);_i<(last);++_i){cout<<#a<<"["<<_i<<"] = "<<(a)[_i]<<endl;}
#define PRINTI(a,i)\
    cout<<"func "<<__func__<<": line "<<__LINE__<<": "<<#a<<"["<<#i<<"] = "<<#a<<"["<<(i)<<"] = "<<(a)[i]<<endl;
#else
#define PRINT(x)
#define PRINTA(a,first,last)
#define PRINTI(a,i)
#endif

#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);--i)
#define REP(i,n) for (int i=0;i<(n);++i)
#define RREP(i,n) for (int i=(n)-1;i>=0;--i)
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define MOD 1000000007
//#define MOD 998244353

using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

template <class T, class U> void amax(T& x, U y) {if (x < y) x = y;}
template <class T, class U> void amin(T& x, U y) {if (x > y) x = y;}

template <class T, class U>
std::ostream& operator<<(std::ostream &os, const std::pair<T, U> &p) {
	os << "(" << p.first << ", " << p.second << ")";
	return os;
}
 
template <class T>
std::ostream& operator<<(std::ostream &os, const std::vector<T> &v) {
    os << "[";
	for (int i = 0; i < v.size(); ++i) {
        if (i) {
            if (i % 5 == 0) {
                os << ",\n ";
            } else {
                os << ", ";
            }
        }
		os << v[i];
	}
	os << "]";
	return os;
}

template <class T>
using vec2 = vector<vector<T>>;

template <class T>
using vec3 = vector<vector<vector<T>>>;

const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll N, K;
    cin >> N >> K;
    vector<ll> P(N);
    REP(i, N) {
        cin >> P[i];
        --P[i];
    }
    vector<ll> C(N);
    REP(i, N) {
        cin >> C[i];
    }

    ll result = LLONG_MIN;

    vector<bool> used(N, false);
    int cnt = 0;
    REP(start, N) {
        if (!used[start]) {
            used[start] = true;
            int cur = P[start];
            ll cost_period = C[start];
            vector<int> v{start};
            while (cur != start) {
                v.pb(cur);
                PRINT(cur);
                assert(!used[cur]);
                used[cur] = true;
                cost_period += C[cur];
                cur = P[cur];
            }
            int period = v.size();
            assert(period >= 2);
            PRINT(v);
            cnt += period;
            for (int s: v) {
                cur = s;
                cur = P[cur];
                ll cost = C[cur];
                ll k = K - 1;
                PRINT(cost);
                if (k >= 3 * period) {
                    ll x = k / period; // >= 2
                    if (cost_period > 0) {
                        cost += (x - 2) * cost_period;
                        k -= (x - 2) * period;
                    } else {
                        k -= (x - 2) * period;
                    }
                    assert(k >= 2 * period);

                }
                PRINT(cost);
                ll cost_max = cost;
                while (k--) {
                    cur = P[cur];
                    cost += C[cur];
                    amax(cost_max, cost);
                }
                amax(result, cost_max);
            }
        }
    }

    assert(cnt == N);

    cout << result << endl;

    return 0;
}


Measured memory_bytes: 3744