using System;
using static System.Console;
using System.Collections.Generic;

namespace abc175
{
    class Program
    {
        static long[] ReadNumList()
        {
            var line = Console.ReadLine().Split(' ');
            if (line.Length == 1 && string.IsNullOrEmpty(line[0]))
            {
                return new long[0];
            }
            var numList = new long[line.Length];
            for (var i = 0; i < line.Length; ++i)
            {
                numList[i] = long.Parse(line[i]);
            }
            return numList;
        }
        static void Main()
        {
            var nk = ReadLine().Split(' ');
            var n = int.Parse(nk[0]);
            var k = int.Parse(nk[1]);
            var pList = ReadNumList();
            var cList = ReadNumList();

            WriteLine(MP(n, k, pList, cList));
        }
        static long MP(int n, int k, long[] pList, long[] cList)
        {
            // divide
            var loopList = new List<long[]>();
            var scoreList = new List<long[]>();
            var totalList = new List<long>();
            var counted = new bool[n];
            for (var i = 0L; i < n; ++i)
            {
                if (!counted[i])
                {
                    var loop = new List<long>();
                    var score = new List<long>();
                    var ptr = i;
                    var total = 0L;
                    while (!counted[ptr])
                    {
                        loop.Add(ptr);
                        score.Add(cList[ptr]);
                        total += cList[ptr];
                        counted[ptr] = true;
                        ptr = pList[ptr] - 1;
                    }
                    loopList.Add(loop.ToArray());
                    scoreList.Add(score.ToArray());
                    totalList.Add(total);
                }
            }

            var max = long.MinValue;
            for (var l = 0; l < loopList.Count; ++l)
            {
                if (totalList[l] > 0)
                {
                    // k/s loops + maxsubtotal(k%s)
                    var loops = k / loopList[l].Length;
                    max = Math.Max(max,
                        totalList[l] * loops + MaxSubTotal(scoreList[l], k % loopList[l].Length));
                    max = Math.Max(max,
                        totalList[l] * (loops - 1) + MaxSubTotal(scoreList[l], Math.Min(k, loopList[l].Length)));
                }
                else
                {
                    // maxsubtotal(max(k, s))
                    max = Math.Max(max,
                        MaxSubTotal(scoreList[l], Math.Min(k, loopList[l].Length)));
                }
            }
            return max;
        }
        static long MaxSubTotal(long[] scoreList, long maxLength)
        {
            if (maxLength == 0)
            {
                return 0;
            }
            var rui = new long[scoreList.Length * 2 + 1];
            var tmp = 0L;
            for (var i = 0; i < scoreList.Length; ++i)
            {
                rui[i] = tmp;
                tmp += scoreList[i];
            }
            for (var i = 0; i < scoreList.Length; ++i)
            {
                rui[i + scoreList.Length] = tmp;
                tmp += scoreList[i];
            }
            rui[scoreList.Length * 2] = tmp;
            var max = long.MinValue;
            for (var l = maxLength; l > 0; --l)
            {
                for (var i = 0; i < rui.Length - l; ++i)
                {
                    max = Math.Max(max, rui[i + l] - rui[i]);
                }
            }
            return max;
        }
    }
}


Measured memory_bytes: 29572