//#pragma warning(disable:4996)
#include <iostream>
#include <vector>
#include <limits.h>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <bitset>
#include <cassert>
#include <random>
#include <functional>
#include <stack>
#include <iomanip>
#include <cassert>
#include <boost/multiprecision/cpp_int.hpp>
#include <complex>
#include <cstdio>
#include <list>
#include <bitset>
//#include <stdio.h>

//< in.txt > out.txt
using namespace std;
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
typedef long long LL;
typedef long double LD;
typedef boost::multiprecision::cpp_int bigint;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PI;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
template<class T>
using V = vector<T>;
template<class T>
using VV = vector<vector<T>>;
typedef unsigned long long ULL;

template<class T>
struct Gedge {
	int src, to;
	T cost;
	int id;
	Gedge(int s, int t, T c, int i = -1) :src(s), to(t), cost(c), id(i) {}
	Gedge(int t, T c) :src(-1), to(t), cost(c), id(-1) {}
};

template<class T>
using WeightedGraph = vector<vector<Gedge<T>>>;
using UnweightedGraph = vector<vector<LL>>;
template<class T>
using Gedges = vector<Gedge<T>>;

template<class T>
struct TNode {
	int parent;
	VI childs;
	T cost;
	int id;
	TNode() :parent(-1), cost(0), id(-1) {};
};
template<class T>
using Tree = vector<TNode<T>>;

//到達不可能->INF
template<class T>
void dijkstra(WeightedGraph<T>& G, int start, vector<T>& res) {
	res.resize(G.size(), INF);
	res[start] = 0;
	typedef pair<T, int> P;
	priority_queue<P, vector<P>, greater<P>> q;
	q.emplace(0, start);
	while (!q.empty()) {
		int cur;
		T cost;
		tie(cost, cur) = q.top();
		q.pop();
		if (res[cur] < cost)continue;
		for (Gedge<T>& e : G[cur]) {
			if (res[e.to] > cost + e.cost) {
				res[e.to] = cost + e.cost;
				q.emplace(res[e.to], e.to);
			}
		}
	}
	return;
}

int main() {
	std::cin.tie(0);
	std::ios::sync_with_stdio(false);
	int N, K;
	cin >> N >> K;
	VI P(N);
	for (int n = 0; n < N; n++) {
		cin >> P[n];
		P[n]--;
	}
	VI C(N);
	for (int n = 0; n < N; n++) {
		cin >> C[n];
	}
	LL wholeans = numeric_limits<LL>::min();
	for (int n = 0; n < N; n++) {
		LL loopsum = C[n];
		int p = P[n];
		int count = 1;
		while (p != n) {
			loopsum += C[p];
			count++;
			p = P[p];
		}
		LL sum = C[n];
		LL smax = C[n];
		p = P[n];
		int c = 1;
		while (p != n && c < K) {
			sum += C[p];
			smax = max(smax, sum);
			p = P[p];
			c++;
		}
		wholeans = max(wholeans, smax);
		if (K >= count && loopsum > 0) {
			int k = 0;
			int t = K / count;
			LL summax = 0;
			LL sum = 0;
			p = n;
			while (count * t + k <= K) {
				summax = max(summax,
					loopsum * t + sum);
				k++;
				sum += C[p];
				p = P[p];
			}
			t--;
			while (p != n && count * t + k <= K) {
				summax = max(summax,
					loopsum * t + sum);
				k++;
				sum += C[p];
				p = P[p];
			}
			wholeans = max(wholeans, summax);
		}
	}
	cout << wholeans << "\n";
	return 0;
}


Measured memory_bytes: 3172