using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace contest_Csharp
{

    class Program
    {
        public static (long m, long s)[,] dp;

        static void Main(string[] args)
        {
            checked
            {
                var sw = new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
                Console.SetOut(sw);

                var vals = Console.ReadLine().Split(' ').
                    Select(x => long.Parse(x)).ToList();


                var N = vals[0];
                var K = vals[1];

                var P = Console.ReadLine().Split(' ').
                    Select(x => int.Parse(x)).ToList();
                var C = Console.ReadLine().Split(' ').
                    Select(x => long.Parse(x)).ToList();

                var checker = new bool[N];

                var groups = new List<(int start, long sum, int num,
                    List<long> ruiseki)>();
                for (int i = 0; i < N; i++)
                {
                    if (!checker[i])
                    {
                        var pt = i;
                        var st = i;
                        long sm = 0;
                        var nm = 0;
                        var rui = new List<long>();
                        rui.Add(0);
                        do
                        {
                            checker[pt] = true;

                            sm += C[pt];
                            pt = P[pt] - 1;
                            rui.Add(rui[nm] + C[pt]);
                            nm++;
                        }
                        while (pt != i);
                        groups.Add((i, sm, nm, rui));
                    }
                }
                long ans = long.MinValue + 100;
                long lnt = -10;
                foreach (var g in groups)
                {
                    long pa = long.MinValue + 100;
                    for (int i = 0; i < g.num; i++)
                    {


                        for (int k = i; k < g.num + 1; k++)
                        {

                            /*int f = 1;
                            if (g.sum < 0)
                                f = 0;*/

                            if (k - i <= K)
                            {
                                /*if (pa < g.ruiseki[k] - g.ruiseki[i]
                                      + (K - (k - i)) / g.num * Math.Max(0, g.sum))
                                    lnt = k - i + (K - (k - i)) / g.num * g.num * f;*/
                                pa = Math.Max(pa, g.ruiseki[k] - g.ruiseki[i]
                                    + (K - (k - i)) / g.num * Math.Max(0, g.sum));

                            }
                            if (i + g.num - k <= K)
                            {
                                /*if (pa < g.ruiseki[k] + g.sum
                                    + (K - (g.num - k + i)) / g.num * Math.Max(0, g.sum))
                                    lnt = i + g.num - k + (K - (g.num - k + i)) / g.num * g.num * f;*/
                                pa = Math.Max(pa, g.ruiseki[i] - g.ruiseki[k] + g.sum
                                    + (K - (g.num - k + i)) / g.num * Math.Max(0, g.sum));
                            }


                        }
                    }

                    ans = Math.Max(ans, pa);
                    if (C.Max()<=0)
                    {
                        ans = C.Max();
                    }

                }

                //L.Sort();








                Console.WriteLine(ans);

                Console.Out.Flush();
            }




        }
    }

}




Measured memory_bytes: 29624