#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pi 3.141592653589793238
#define eb emplace_back
#define rep(i,a,b) for (int i = a; i <= b; i++)
#define zip(i,a,b) for(int i=a;i<b;i++)
#define rzip(i,a,b) for(int i=a;i>=b;i--)
#define ll unsigned int
#define test int t;cin>>t; while(t--)
#define um unordered_map
#define en '\n'
#define us unordered_set
typedef pair<int, int>     pii;
typedef pair<char, int>    pci;
typedef pair<char, char>   pcc;
typedef vector<pii>       vii;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define sz(x) (int)x.size()
#define INF (1e18+5)
#define inf (1e9+5)
#define mod 1000000007
#define minf -10000000000000


void __print(int x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
// You should only debug a pair of simple data types. For example,
// the debug won't work if one of pair's elements is collection type
// (std::vector, std::map, std::set...).
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif


// bool check_prime(int n)  return whether a number is prime or not O(sqrt(N));
// int BE(int x,int n,int m)  return x^n%m; O(logN);
// void sieve()  from number 1-1000001 saare prime store kar lega  in bool is_prime[1000001] array
// vector<int> z_array(string s)  return vector which is Z-array of string s;
// vector<int> lps(string s)      return vector which is lps array of string s;
// int power(int x,int n) return x^n; O(logN);
// using_ordered_set (template for using ordered set , replace first 2 lines of this page with this code..);

vi v[5009];
int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(0);
#ifndef ONLINE_JUDGE
	if (fopen("input.txt", "r"))
	{
		freopen ("input.txt" , "r" , stdin);
		freopen ("output.txt" , "w" , stdout);
	}
#endif
	//_______________________________-code starts-_______________________________________________
	int n, k;
	cin >> n >> k;
	int p[n + 1];
	int c[n + 1];
	rep(i, 1, n)cin >> p[i];
	rep(i, 1, n)cin >> c[i];
	//int ans = 0;

	rep(j, 1, n)
	{
		// starting from index i..
		unordered_map<int, int>m1;
		int i = j;
		while (true)
		{
			if (m1[i] > 0)
				break;
			m1[i]++;
			v[j].pb(c[p[i]]);
			i = p[i];
		}
		m1.clear();
	}
	// rep(i, 1, n)
	// {
	// 	cout << i << " -> ";
	// 	for (auto x : v[i])cout << x << " "; cout << en;
	//}
	rep(i, 1, n)
	{
		zip(j, 1, sz(v[i]))
		{
			v[i][j] += v[i][j - 1];
		}
	}
	// cout << en << en;
	// rep(i, 1, n)
	// {
	// 	cout << i << " -> ";
	// 	for (auto x : v[i])cout << x << " "; cout << en;
	// }

	//debug(v);
	int ans = minf;
	//debug(ans);
	rep(i, 1, n)
	{
		int m = sz(v[i]);
		int var1 = (k / m) * v[i][m - 1];
		int var4 = minf;
		if (k / m > 2)
		{
			//var1=(k/m-1)*v[i][m-1];
			int lodha = m + k % m;
			int lassan = minf;
			zip(j, 0, min(m, lodha))
			{
				lassan = max(lassan, v[i][j]);
			}
			var4 = (k / m - 1) * v[i][m - 1] + lassan;

		}
		if (k < m)
		{
			var1 = minf;
		}
		int var = (k % m);
		int var2 = minf;
		int var3 = minf;
		zip(j, 0, var)
		{
			if (var1 > 0)
				var2 = max(var2, v[i][j]);
			if (var1 > 0)
				var3 = max(var3, var1 + var2);
		}
		zip(j, 0, min(k, m))
		{
			var2 = max(var2, v[i][j]);
		}
		ans = max({ans, var1, var2, var3, var4});
	}
	cout << ans << en;


	return 0;
}




Measured memory_bytes: 219096