#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <cmath>

#define llong long long
#define rep(i,l,n) for(llong (i)=(l);(i)<(n);(i++))
#define _min(a,b) ((a)<(b)?(a):(b))
#define _max(a,b) ((a)>(b)?(a):(b))
#define _abs(a) ((a)>0?(a):(-(a)))

using namespace std;

llong max3(llong*,llong);

int main(){
	int N,K;
	cin >> N >> K;
	int P[N],C[N];
	rep(i,0,N){
		cin >> P[i];
		P[i] -= 1;
	}
	rep(i,0,N) cin >> C[i];
	
	llong ans=-1000000000;
	rep(i,0,N){
		int n = i;
		llong score = 0,max = C[P[n]];
		rep(j,1,K+1){
			n = P[n];
			score += C[n];
			max = _max(max,score);
			if( n == i ){
				if( score < 0 ) break;
              	max = _max(max,max+score*((K/j)-1));
				score *=(K/j);
				j = (K/j)*j;
			}
		}
		ans = _max(ans,max);
	}
	
	cout << ans;
}
