// D - Moving Piece
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(int)(n);++i)

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

	int N, K; cin>>N>>K;
	vector<int> P(N), C(N);
	rep(i, N) cin>>P[i], P[i]--;
	rep(i, N) cin>>C[i];

	ll score = -1e18;
	rep(st, N){
		ll lap_sc = 0;
		int lap_cn = 0;
		int nx = st;
		while(true){
			lap_cn++;
			lap_sc += C[nx];
			if(st == (nx = P[nx])) break;
		}
		lap_sc = max(0LL, lap_sc);

		ll sum_sc = 0;
		rep(k_cn, K){
			sum_sc += C[nx];
			score = max(score, sum_sc + lap_sc * ((K - (k_cn + 1)) / lap_cn));
			if(st == (nx = P[nx])) break;
		}
	}
	cout<< score <<endl;
}
