#include "bits/stdc++.h"
#include <boost/multi_array.hpp>
#include <boost/optional.hpp>
#include <boost/range/irange.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/dynamic_bitset.hpp>
namespace adaptor = boost::adaptors;
using index_t = std::size_t;

namespace lib
{
	using graph_t = std::vector<std::set<index_t>>;
	template<class T>using weighted_graph_t = std::vector<std::map<index_t, T>>;

	struct max_t
	{
		template<class T>constexpr T operator()(T const& lhs, T const& rhs)const
		{
			return std::max(lhs, rhs);
		}
	};
	struct min_t
	{
		template<class T>constexpr T operator()(T const& lhs, T const& rhs)const
		{
			return std::min(lhs, rhs);
		}
	};

	constexpr max_t max;
	constexpr min_t min;

	template<class T>auto order_by(T func)
	{
		return [=](auto&& lhs, auto&& rhs)
		{
			return func(lhs) < func(rhs);
		};
	}

	template<class T>constexpr auto max_v = std::numeric_limits<T>::max();
	template<class T>constexpr auto min_v = std::numeric_limits<T>::min();

	template<class T>using p_queue = std::priority_queue<T, std::vector<T>, std::greater<>>;
	
	template<class T>constexpr T pow(T val, std::uint64_t p)
	{
		auto ret = T(1);
		auto s = 1ULL;
		for (auto i = 0; i < 63; ++i, s <<= 1)
		{
			if (p & s)
			{
				ret *= val;
			}
			val *= val;
		}
		return ret;
	}
}

template<class T>struct max_value
{
	T v;
	max_value(T u = lib::min_v<T>) :v(u)
	{

	}
	auto& operator=(T const& u)
	{
		v = std::max(v, u);
		return *this;
	}
	auto& operator=(max_value const& u)
	{
		v = std::max(v, *u);
		return *this;
	}
	T operator*()const
	{
		return v;
	}
	void reset()
	{
		v = lib::min_v<T>;
	}
};

struct function_t
{
	std::size_t N;
	std::vector<std::int64_t> points;
	std::vector<index_t> nexts;
	std::vector<std::optional<std::pair<std::int64_t, std::size_t>>> loop;

	std::pair<std::int64_t, std::size_t> check_loop(index_t idx, index_t start, std::int64_t point, std::size_t depth)
	{
		if (idx == start)
		{
			return std::make_pair(std::max(point, std::int64_t()), depth);
		}
		return *(loop[idx] = check_loop(nexts[idx], start, point + points[idx], depth + 1));
	}

	std::int64_t run(std::size_t K)
	{
		for (auto i : boost::irange(N))
		{
			if (!loop[i])
			{
				loop[i] = check_loop(nexts[i], i, points[i], 1);
			}
		}
		max_value<std::int64_t> ret;
		for (auto start : boost::irange(N))
		{
			auto idx = start;
			std::int64_t sum = 0;
			auto rest = K;
			auto [p, len] = *loop[idx];
			auto c = std::max(rest / len - 2, std::size_t());
			rest -= c * len;
			sum += p * c;
			for (;  0 < rest; --rest)
			{
				idx = nexts[idx];
				ret = sum += points[idx];
			}
		}
		return *ret;
	}
};

void Main()
{
	std::size_t N, K;
	std::cin >> N >> K;
	std::vector<index_t> nexts(N);
	std::vector<std::int64_t> points(N);
	for (auto& n : nexts)
	{
		std::cin >> n;
		--n;
	}
	for (auto& p : points)
	{
		std::cin >> p;
	}
	function_t func{ N,std::move(points),std::move(nexts),std::vector<std::optional<std::pair<std::int64_t, std::size_t>>>(N) };
	std::cout << func.run(K) << std::endl;
}

int main()
{
	std::cin.tie(nullptr);
	std::ios_base::sync_with_stdio(false);
	std::cout << std::fixed << std::setprecision(15);
	Main();
}

Measured memory_bytes: 3908