using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace AtCoder.D
{
    public class Program
    {
        public static void Main() { var r = GetResult(); Debug.WriteLine(r); Console.Write(r); }

        private static object GetResult()
        {
            var (N, K) = Read2Longs();
            // インデックス変換
            var P = ReadLongs().Select(x => (int)x - 1).ToList();
            var C = ReadLongs();

            // すべて0以下の場合
            if (C.All(x => x <= 0))
            {
                // 一度だけ最大の場所に移動するのが最適
                return C.Max();
            }

            var took = new bool[N];
            var loops = new List<List<int>>();
            for (var i = 0; i < N; i++)
            {
                if (took[i]) continue;

                var loop = new List<int>();
                var next = P[i];
                while (!took[P[next]])
                {
                    loop.Add(next);
                    next = P[next];
                    took[next] = true;
                }
                loops.Add(loop);
            }

            var max = 0L;
            foreach (var loop in loops)
            {
                var score = GetScore(loop, C, K);
                max = Math.Max(max, score);
            }

            return max;
        }

        private static long GetScore(List<int> loop, List<long> c, long count)
        {
            var sum = loop.Select(x => c[x]).Sum();

            // 1周が最適な場合
            if (sum <= 0 || count <= loop.Count)
            {
                var maxScrore = 0L;
                for (var start = 0; start < loop.Count; start++)
                {
                    var currentScoreSum = 0L;
                    var limit = Math.Min(loop.Count, count);
                    for (var i = 0; i < limit; i++)
                    {
                        var next = start + i;
                        if (next >= loop.Count)
                        {
                            next -= loop.Count;
                        }
                        currentScoreSum += c[loop[next]];

                        maxScrore = Math.Max(maxScrore, currentScoreSum);
                    }
                }
                return maxScrore;
            }

            // 1周以上が最適な場合
            var score = sum * (count / loop.Count);
            var nokoriCount = count % loop.Count;

            // 最後の一周で油断しない
            if (nokoriCount == 0)
            {
                score -= sum;
                nokoriCount += loop.Count;
            }

            return score + GetScore(loop, c, nokoriCount);
        }

        #region Console.Read

        public static string ReadText() { return Console.ReadLine(); }
        public static List<string> ReadTexts() { return Console.ReadLine().Split(' ').ToList(); }
        public static long ReadLong() { return long.Parse(Console.ReadLine()); }
        public static (long, long) Read2Longs() { var ls = ReadLongs(); return (ls[0], ls[1]); }
        public static (long, long, long) Read3Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2]); }
        public static (long, long, long, long) Read4Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2], ls[3]); }
        public static (long, long, long, long, long) Read5Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2], ls[3], ls[4]); }
        public static List<long> ReadLongs() { return Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToList(); }

        #endregion
    }
}


Measured memory_bytes: 29716