using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Pipes;
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();
            var C = ReadLongs();
            if (C.All(x => x <= 0))
            {
                return C.Max();
            }
            var hash = new HashSet<long>();
            var max = 0L;

            for (var i = 0; i < N; i++)
            {
                if (hash.Contains(i)) { continue; }
                var k = new List<long>();
                var currentIndex = i;

                while (!hash.Contains(currentIndex))
                {
                    hash.Add(currentIndex);
                    k.Add(C[currentIndex]);
                    currentIndex = (int)P[currentIndex] - 1;
                }
                max = Math.Max(max, GetScore(k, K));
            }
            return max;
        }

        private static long GetScore(List<long> l, long K)
        {
            var count = l.Count();
            var mod = K % count == 0 ? count : K % count;
            var sum = l.Sum();
            if (sum < 0 && K >= count)
            {
                mod = count;
            }

            var max = 0L;
            for (var j = 0; j < mod; j++)
            {
                var ans = 0L;
                for (var i = 0; i < count; i++)
                {
                    if (i == 0)
                    {
                        for (var k = 0; k < j + 1; k++)
                        {
                            ans += l[i + k];
                        }
                    }
                    else
                    {
                        ans -= l[i - 1];
                        ans += l[j + i >= count ? j + i - count : j + i];
                    }
                    max = Math.Max(max, ans);
                }
            }
            if (sum > 0)
            {
                return max + sum * ((K - mod) / count);
            }
            else
            {
                return max;
            }
        }

        #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(); }
        public static Dictionary<long, long> ReadLongsDic()
        {
            var i = -1L;
            return Console.ReadLine().Split(' ')
                .Select(x => long.Parse(x)).ToDictionary(x =>
                {
                    i++;
                    return i;
                });
        }

        #endregion
    }
}


Measured memory_bytes: 29828