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

namespace ABC175_D
{
    using static Ex;
    public class D
    {
        public static void Main(string[] args)
        {
            new ABC();
        }
    }

    public class ABC
    {
        long n, k;
        int[] p, c;
        public ABC()
        {
            (n, k) = ReadLongArray().ToTpl2();
            p = ReadIntArray();
            c = ReadIntArray();
            var used = new int[n];
            var dict = new Dictionary<int, (long, int)>();
            long max = long.MinValue;
            for (int i = 0; i < n; i++)
            {
                if (used[i] > 0) continue;
                int cou = 0;
                long tmp = 0;
                int j = i;
                int key = p[i];
                while (true)
                {
                    used[j] = key;
                    j = p[j] - 1;
                    tmp += c[j];
                    cou++;
                    if (j == i) break;
                }
                dict[key] = (tmp, cou);
            }
            for (int i = 0; i < n; i++)
            {
                long cou = k;
                long tmp = 0;
                int j = i;
                int key = used[i];
                if (dict[key].Item1 > 0)
                {
                    var d = dict[key].Item2;
                    var div = k / d;
                    var mod = k % d;
                    cou = mod + d; // +d => 最後の1ループ
                    tmp = (div - 1) * dict[key].Item1;
                    //$"i:{j} {tmp} {cou}".Cw();
                    while (cou > 0)
                    {
                        j = p[j] - 1;
                        tmp += c[j];
                        Maxs(ref max, tmp);
                        cou--;
                        //$"{j} {tmp} {cou}".Cw();
                    }
                }
                else
                {
                    while (cou > 0)
                    {
                        j = p[j] - 1;
                        tmp += c[j];
                        Maxs(ref max, tmp);
                        cou--;

                        if (j == i) break;
                    }
                }
            }
            max.Cw();
        }
    }

    public static class Ex
    {
        public static void Cw(this object s) { Console.WriteLine(s); }



        public static bool Mins<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) > 0) { a = b; return true; } return false; }
        public static bool Maxs<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; }
        public static void Swap<T>(ref T a, ref T b) { var t = b; b = a; a = t; }
        public static void Swap<T>(List<T> list, int i, int j) { T tmp = list[j]; list[j] = list[i]; list[i] = tmp; }

        public static long Lcm(int x, int y) { return x / Gcd(x, y) * y; }
        public static long Gcd(long m, long n) { if (n == 0) return m; return Gcd(n, m % n); }
        public static List<string> FastSort(this List<string> s) { s.Sort(StringComparer.Ordinal); return s.ToList(); }
        public static void yesno(this bool b) { (b ? "yes" : "no").Cw(); }
        public static void YesNo(this bool b) { (b ? "Yes" : "No").Cw(); }
        public static void YESNO(this bool b) { (b ? "YES" : "NO").Cw(); }

        public static string Read { get { return Console.ReadLine().Trim(); } }
        public static int ReadInt { get { return int.Parse(Read); } }
        public static long ReadLong { get { return long.Parse(Read); } }
        public static double ReadDouble { get { return double.Parse(Read); } }
        public static string[] ReadArray() { return Read.Split(' '); }
        public static int[] ReadIntArray() { return Read.Split(' ').Select(int.Parse).ToArray(); }
        public static long[] ReadLongArray() { return Read.Split(' ').Select(long.Parse).ToArray(); }
        public static string[] ReadArray(long N) { var v = new string[N]; for (long i = 0; i < N; ++i) v[i] = Read; return v; }
        public static int[] ReadIntArray(long N) { var v = new int[N]; for (long i = 0; i < N; ++i) v[i] = ReadInt; return v; }
        public static long[] ReadLongArray(long N) { var v = new long[N]; for (long i = 0; i < N; ++i) v[i] = ReadLong; return v; }

        public static (T, T) ToTpl2<T>(this T[] v) { return (v[0], v[1]); }
        public static (T, T, T) ToTpl3<T>(this T[] v) { return (v[0], v[1], v[2]); }
        public static (T, T, T, T) ToTpl4<T>(this T[] v) { return (v[0], v[1], v[2], v[3]); }
        public static (T, T, T, T, T) ToTpl5<T>(this T[] v) { return (v[0], v[1], v[2], v[3], v[4]); }
    }
}


Measured memory_bytes: 29544