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# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using static System.Math;

namespace CSharpSpiral {
	class Program {
		public static void Main(string[] args) {
			var sw = new StreamWriter(Console.OpenStandardOutput()) {
				AutoFlush =
#if DEBUG
			true
#else
			false
#endif
			};
			Console.SetOut(sw);
			new Program().Solve(new ConsoleInput(
#if DEBUG
			new StreamReader("../../../inputData.txt")
#else
			Console.In
#endif
			, ' '));
			Console.Out.Flush();
		}
		public void Solve(ConsoleInput cin) {
			int N = cin.ReadInt, K = cin.ReadInt;
			var P = cin.ReadIntArray(N);
			var C = cin.ReadLongArray(N);
			var groups = Enumerable.Repeat(-1, N).ToArray();
			var scoreChain = new List<long>[N];
			var groupNo = 0;
			scoreChain[0] = new List<long>();
			for(int i = 0; i < N; i++) {
				if(groups[i] >= 0) continue;
				var j = i;
				while(groups[j] < 0) {
					groups[j] = groupNo;
					scoreChain[groupNo].Add(C[j]);
					j = P[j] - 1;
				}
				groupNo++;
				scoreChain[groupNo] = new List<long>();
			}
			var res = scoreChain.Take(groupNo).Max(l => loopMax(l, K));
			Console.WriteLine(res);
		}

		public long loopMax(List<long> X, int K) {
			var N = X.Count;
			var XX = new long[2 * N + 1];
			//XX[i] = sum(X[0...i])
			for(int i = 0; i < 2 * N; i++) {
				XX[i + 1] = XX[i] + X[i % N];
			}
			var countMax = new long[N]; //1 time, .., N times
			for(int j = 0; j < N; j++) {
				var j1 = j + 1;
				//(0...j1),(1...j1+1), .. , (N-j1...N) in X
				countMax[j] = Enumerable.Range(0, N).Select(k => XX[k + j1] - XX[k]).Max();
			}
			if(K < N) {
				return countMax.Take(K).Max();
			}
			long lapGain = X.Sum();
			if(lapGain <= 0) {
				return countMax.Max();
			} else {
				var rem = K % N;
				var loopc = K / N;
				var R = countMax.Take(rem).Concat(countMax.Skip(rem).Select(c => c - lapGain));
				var Res = R.Max() + loopc * lapGain;
				return Res;
			}
		}

	}

	static class ExtendClass {
		public static string Join<T>(this IEnumerable<T> X, string separator = " ")
			=> string.Join(separator, X);
	}
	class ConsoleInput {
		private readonly TextReader _stream;
		private readonly char _separator = ' ';
		private readonly Queue<string> inputStream;
		public ConsoleInput(TextReader stream, char separator = ' ') {
			this._separator = separator;
			this._stream = stream;
			inputStream = new Queue<string>();
		}
		public string Read {
			get {
				if(inputStream.Count != 0) return inputStream.Dequeue();
				string[] tmp = _stream.ReadLine().Split(_separator);
				for(int i = 0; i < tmp.Length; ++i)
					inputStream.Enqueue(tmp[i]);
				return inputStream.Dequeue();
			}
		}
		public string ReadLine { get { return _stream.ReadLine(); } }
		public int ReadInt { get { return int.Parse(Read); } }
		public long ReadLong { get { return long.Parse(Read); } }
		public double ReadDouble { get { return double.Parse(Read); } }
		public string[] ReadStrArray(long N) {
			var ret = new string[N];
			for(long i = 0; i < N; ++i)
				ret[i] = Read;
			return ret;
		}
		public int[] ReadIntArray(long N) { var ret = new int[N]; for(long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
		public long[] ReadLongArray(long N) { var ret = new long[N]; for(long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; }
		public double[] ReadDoubleArray(long N) { var ret = new double[N]; for(long i = 0; i < N; ++i) ret[i] = ReadDouble; return ret; }
		public bool isEOF {
			get { return _stream.Peek() == -1; }
		}
	}

	class SegmentTree<T> {
		//UI is 0-indexed, internal is 1-indexed
		public readonly int rank;
		public T[] Nodes { get; }
		public Func<T, T, T> OPER { get; }
		public T unit { get; set; }
		protected int width { get { return 1 << (rank - 1); } }
		private T raise(int c) {
			return (c & 1) == 0
			? OPER(Nodes[c - 1], Nodes[(c - 1) + 1]) //$c is even
			: OPER(Nodes[(c - 1) - 1], Nodes[c - 1]); //odd
		}
		public IEnumerable<T> leafs() => Nodes.Skip(width - 1);
		protected int leafIndex(int x) => (1 << (rank - 1)) + (x - 1);
		public SegmentTree(int count, Func<T, T, T> _OPER, T _unit) {
			if(count == 0) throw new ArgumentNullException();
			var last = count - 1;
			rank = 0;
			if(last > 0) {
				while((last >> rank) > 1) rank++;
				rank += 2;
			} else rank = 1;
			Nodes = Enumerable.Repeat(_unit, (1 << rank) - 1).ToArray();
			OPER = _OPER;
			unit = _unit;
		}
		public virtual void SetImmediate(int x, T value) {
			var r = rank - 1;
			var jc = (1 << r) + x;
			Nodes[jc - 1] = value;
			for(r--; r >= 0; r--) {
				var parentIndex = (jc >> 1) - 1;
				var oldValue = Nodes[parentIndex];
				Nodes[parentIndex] = raise(jc);
				if(oldValue.Equals(Nodes[parentIndex])) break;
				jc >>= 1;
			}
		}
		public virtual void ImmediateOperate<S>(int x, S value, Func<S, T, T> LeftOP) {
			SetImmediate(x, LeftOP(value, Nodes[leafIndex(x)]));
		}
		public virtual T GetValue(int start, int end) => GV(start, end, 1, 0, width);
		private T GV(int start, int end, int count, int focusStart, int focusEnd) {
			if(focusEnd <= start || end <= focusStart) return unit;
			if(start <= focusStart && focusEnd <= end) return Nodes[count - 1];
			var mid = focusStart + (focusEnd - focusStart) / 2;
			return OPER(
				GV(start, end, count * 2, focusStart, mid),
				GV(start, end, count * 2 + 1, mid, focusEnd)
			);
		}
		public IEnumerable<T> GetLayer(int c) => Nodes.Skip((1 << (c - 1)) - 1).Take(1 << (c - 1));
		public IEnumerable<T> GetLeaves() => Nodes.Skip((1 << (rank - 1)) - 1).Take(1 << (rank - 1));
	}
}