{"raw_statement":[{"iden":"problem statement","content":"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$.\nNow, 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):\n\n*   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}$.\n\nHelp him by finding the maximum possible score at the end of the game. (The score is $0$ at the beginning of the game.)"},{"iden":"constraints","content":"*   $2 \\leq N \\leq 5000$\n*   $1 \\leq K \\leq 10^9$\n*   $1 \\leq P_i \\leq N$\n*   $P_i \\neq i$\n*   $P_1, P_2, \\cdots, P_N$ are all different.\n*   $-10^9 \\leq C_i \\leq 10^9$\n*   All values in input are integers."},{"iden":"input","content":"Input is given from Standard Input in the following format:\n\n$N$ $K$\n$P_1$ $P_2$ $\\cdots$ $P_N$\n$C_1$ $C_2$ $\\cdots$ $C_N$"},{"iden":"sample input 1","content":"5 2\n2 4 5 1 3\n3 4 -10 -8 8"},{"iden":"sample output 1","content":"8\n\nWhen we start at some square of our choice and make at most two moves, we have the following options:\n\n*   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$.\n*   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$.\n*   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$.\n*   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$.\n*   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$.\n\nThe maximum score achieved is $8$."},{"iden":"sample input 2","content":"2 3\n2 1\n10 -7"},{"iden":"sample output 2","content":"13"},{"iden":"sample input 3","content":"3 3\n3 1 2\n-1000 -2000 -3000"},{"iden":"sample output 3","content":"\\-1000\n\nWe have to make at least one move."},{"iden":"sample input 4","content":"10 58\n9 1 6 7 8 4 3 2 10 5\n695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719"},{"iden":"sample output 4","content":"29507023469\n\nThe absolute value of the answer may be enormous."}],"translated_statement":null,"sample_group":[],"show_order":["default"],"formal_statement":null,"simple_statement":null,"has_page_source":true}