Problem:
Score : 400 points

Problem Statement

Takahashi will play a game using a piece on an array of squares numbered 1, 2, \cdots, N. Square i has an integer C_i written on it. Also, he is given a permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.

Now, he will choose one square and place the piece on that square. Then, he will make the following move some number of times between 1 and K (inclusive):

In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.

Help him by finding the maximum possible score at the end of the game. (The score is 0 at the beginning of the game.)

Constraints

2 \leq N \leq 5000

1 \leq K \leq 10^9

1 \leq P_i \leq N

P_i \neq i

P_1, P_2, \cdots, P_N are all different.

-10^9 \leq C_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N

Output

Print the maximum possible score at the end of the game.

Sample Input 1

5 2
2 4 5 1 3
3 4 -10 -8 8

Sample Output 1

8

When we start at some square of our choice and make at most two moves, we have the following options:

If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.

If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.

If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.

If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.

If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.

The maximum score achieved is 8.

Sample Input 2

2 3
2 1
10 -7

Sample Output 2

13

Sample Input 3

3 3
3 1 2
-1000 -2000 -3000

Sample Output 3

-1000

We have to make at least one move.

Sample Input 4

10 58
9 1 6 7 8 4 3 2 10 5
695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719

Sample Output 4

29507023469

The absolute value of the answer may be enormous.

Solution in C# (Mono-csc 3.5.0) (judge verdict: Wrong Answer)

using System; using System.Collections.Generic; using System.Linq; using System.Text;
using System.Text.RegularExpressions;
using static System.Math;
namespace A
{
    class Program
    {
        static void Main()
        {
            var NK = CinTAr;
            var P = CinTAr;
            var C = CinTAr;
            bool[] clis = new bool[NK[0]];
            List<List<Pair<long>>> loops = new List<List<Pair<long>>>();
            int nowL = -1;
            List<decimal> sums = new List<decimal>();
            AFOR(NK[0], (i) =>
            {
                if (!clis[i])
                {
                    loops.Add(new List<Pair<long>>());
                    nowL = loops.Count - 1;
                    long nowtr = i;
                    decimal ltot = 0;
                    while (!clis[nowtr])
                    {
                        ltot += C[nowtr];
                        loops[nowL].Add(new Pair<long>(nowtr, C[nowtr]));
                        clis[nowtr] = true;
                        nowtr = P[nowtr] - 1;
                    }
                    sums.Add(ltot);
                    
                }
            });
            decimal m_tot = decimal.MinValue;
            AFOR(loops.Count, i =>
            {
                int inx = (int)i;
                var par = loops[inx];
                long lc = NK[1] / par.Count;
                long mod = NK[1] % par.Count;
                
                decimal sum = sums[inx];
                decimal tot = 0;
                bool isZero = true;
                if(sum >= 0)
                {//正ならやれるだけやれ
                    tot = sum * lc;
                    isZero = false;
                }
                decimal mppm = decimal.MinValue;
                if(!isZero) { mppm = 0; }
                for(int k = 0;k < par.Count; k++)
                {
                    decimal s = 0;
                    for(int j = isZero? 0: 1;j <= mod; j++)
                    {
                        s += par[Loop(k + j, par.Count - 1)].b;
                        mppm = Max(mppm, s);
                    }
                }
                m_tot = Max(m_tot, tot + mppm);
            });
            Cout = m_tot;
            Wait();
        } //main

        static int MOD10_9_7 = (1000000000 + 7);
        static Random RAN = new Random();
        static string Yes = "Yes", No = "No";
        static char[] AlphTab = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
        static int GetDigits(long n) { return (int)Floor(Log10(n)) + 1; }
        static int GetNumByDigit(long n, int d) { return (int)((n / ((int)Pow(10, d - 1))) % 10); }
        static long Loop(long n, long max)
        {
            if (n > max) { return n % (max + 1); }else if (n < 0) { return  max - (-n - 1) % ( max + 1); }else { return n; }
        }
        static int Loop(int n, int max) { return (int)Loop((long)n, max); }
        static long PW10(int n) { return (long)Pow(10, n); }
        static long GCD(long a, long b) { if (a < b) { return GCD(b, a); } if (a % b == 0) { return b; } else { return GCD(b, a % b); } }
        static long LCM(long a, long b) { return a * b / GCD(a, b); }
        static void Swap<T>(ref T a, ref T b) { T t = a; a = b; b = t; }
        static void SwapArE<T>(T[] a, int i, int j) { T t = a[i]; a[i] = a[j]; a[j] = t; }
        static void SwapLiE<T>(List<T> a, int i, int j) { T t = a[i]; a[i] = a[j]; a[j] = t; }
        static bool IsInt(double d) { return (int)d == d; }
        static string Wait() { return Console.ReadLine(); }
        static string Cin { get { return Wait(); } }
        static string[] CinAr { get { return Cin.Split(' ').Where(x => x != "").ToArray(); } }
        static int CinT { get { return S2i(Cin); } }
        static int[] CinTAr { get { return CinAr.Select(x => S2i(x)).ToArray(); } }
        static long CinL { get { return S2L(Cin); } }
        static long[] CinLAr { get { return CinAr.Select(x => S2L(x)).ToArray(); } }
        static double CinD { get { return S2D(Cin); } }
        static double[] CinDAr { get { return CinAr.Select(x  => S2D(x)).ToArray(); } }
        static int S2i(string s) { return int.Parse(s); }
        static long S2L(string s) { return long.Parse(s); }
        static double S2D(string s) { return double.Parse(s); }
        static object Cout{ set { Console.WriteLine(value); } }
        static object WCout{ set { Console.Write(value); } }
        static string RevStr(string s) { return new string(s.Reverse().ToArray()); }
        static void AFOR(int m, Action<int> ActI) { for(int i = 0;i < m; i++) { ActI(i); } }
        static void AFOR(long m, Action<long> ActI) { for(long i = 0;i < m; i++) { ActI(i); } }
        static void AFOR(long m, Action Act) { AFOR(m, i => Act()); }
        static long[] Divisor(long n)
        {
            if (n == 1) { return new long[] { 1 }; } List<long> r = new List<long>() { 1, n };
            for (long i = 2; i <= Sqrt(n); i++){  if (n % i == 0) { r.Add(i); long f = n / i; if (f != i) { r.Add(f); }} } return r.ToArray();
        }
        static IEnumerable<decimal> PrimeFactors(decimal n)
        {
            decimal i = 2, t = n; while (i * i <= n) { if (t % i == 0) { t /= i; yield return i; } else { i++; } } if (t != 1) { yield return t; }
        }
        static bool IsPrime(long n)
        {
            if (n == 1) { return false; } for (long i = 2; i <= Sqrt(n) + 1; i++) { if (n % i == 0) { return false; } } return true;
        }
        static void CoutAr<Type>(IEnumerable<Type> t) { Cout = string.Join(" ", t); }
        static long nCr(int n, int r) { return nPr(n, r) / Factorial(r); }
        static long nPr(int n, int r) { return FactorDiv(n, n - r); }
        static long FactorDiv(int t, int d) { long r = 1; for (int i = t; i > d; i--) { r *= i; } return r; }
        static long Factorial(int i) { if (i <= 1) { return 1; } return i * Factorial(i - 1); }
        static void CoutPaAr<Type>(IEnumerable<Pair<Type>> p) { CoutAr(p.Select(x => $"({x.a}, {x.b}, {x.c})")); }
    }
    public struct Pair<T>
    {
        public T a, b, c;
        public Pair(T _a, T _b, T _c = default(T)) { a = _a; b = _b; c = _c; }
    }
}