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.



Sample input:
5 2
2 4 5 1 3
3 4 -10 -8 8


Sample output:
8

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
k_max = 10001
score = [0] * N

for i in range(N):
  exist_i = [(False, -1)] * k_max # (exist, j)
  exist_i[i] = (True, 0)
  score_i = [-10**9-1] * k_max # iからスタートしてK回操作する場合の累積和
  score_i[0] = C[P[i]-1]
  index = P[i]-1
  for j in range(1, K+1):
    if exist_i[index][0] == True:
      # 既に進んだことがあるマスの場合
      
      # ループ終了インデックスからループ開始インデックスの累積和の差を確認
      loop_score = score_i[j-1]
      if exist_i[index][1] != 0:
        loop_score = score_i[j-1] - score_i[exist_i[index][1] - 1] # 1ループで増えるスコア
#      print("i, j, score_i", i, j, score_i)
#      print("i, j, loop_score", i, j, loop_score)
      if loop_score > 0:
        loop_num = j - exist_i[index][1] # ループする要素数
        num = K-j # 残りの操作数
        tmp_score = score_i[j-1] + num // loop_num * loop_score # ループ前までの累積和
#        print("i, j, tmp_score", i, j, tmp_score)
        tmp_array = [0] * (num % loop_num + 1)
        tmp_array[0] = tmp_score
        for l in range(1, num % loop_num + 1):
          tmp_array[l] = tmp_array[l-1] + C[P[index]-1]
          index = P[index]-1
#        print("i, j, tmp_array", i, j, tmp_array)
        score[i] = max(tmp_array)
      else:
#        print("i, j, score_i", i, j, score_i)
        score[i] = max(score_i)
#      print("i, j, score_i", i, j, score_i)
      break
    else:
      if j == K-1:
        score_i[j] = score_i[j-1] + C[P[index]-1]
        score[i] = max(score_i)
#        print("i, j, score_i", i, j, score_i)
        break   
    score_i[j] = score_i[j-1] + C[P[index]-1]
#    print("i, j, score_i", i, j, score_i)
#    print("i, j, score", i, j, score)
    exist_i[index] = (True, j)
    index = P[index]-1
print(max(score))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
k_max = 10001
score = [0] * N

for i in range(N):
  exist_i = [(False, -1)] * k_max # (exist, j)
  exist_i[i] = (True, 0)
  score_i = [-10**9-1] * k_max # iからスタートしてK回操作する場合の累積和
  score_i[0] = C[P[i]-1]
  index = P[i]-1
  for j in range(1, K):
    if exist_i[index][0] == True:
      # 既に進んだことがあるマスの場合
      
      # ループ終了インデックスからループ開始インデックスの累積和の差を確認
      loop_score = score_i[j-1]
      if exist_i[index][1] != 0:
        loop_score = score_i[j-1] - score_i[exist_i[index][1] - 1] # 1ループで増えるスコア
#      print("i, j, score_i", i, j, score_i)
#      print("i, j, loop_score", i, j, loop_score)
      if loop_score > 0:
        loop_num = j - exist_i[index][1] # ループする要素数
        num = K-j # 残りの操作数
        tmp_score = score_i[j-1] + num // loop_num * loop_score # ループ前までの累積和
#        print("i, j, tmp_score", i, j, tmp_score)
        tmp_array = [0] * (num % loop_num + 1)
        tmp_array[0] = tmp_score
        for l in range(1, num % loop_num + 1):
          tmp_array[l] = tmp_array[l-1] + C[P[index]-1]
          index = P[index]-1
#        print("i, j, tmp_array", i, j, tmp_array)
        score[i] = max(tmp_array)
      else:
#        print("i, j, score_i", i, j, score_i)
        score[i] = max(score_i)
#      print("i, j, score_i", i, j, score_i)
      break
    else:
      if j == K-1:
        score_i[j] = score_i[j-1] + C[P[index]-1]
        score[i] = max(score_i)
#        print("i, j, score_i", i, j, score_i)
        break   
    score_i[j] = score_i[j-1] + C[P[index]-1]
#    print("i, j, score_i", i, j, score_i)
#    print("i, j, score", i, j, score)
    exist_i[index] = (True, j)
    index = P[index]-1
print(max(score))

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
P=list(map(int,input().split()))
P=list(map(lambda x:x-1,P)) 
C=list(map(int,input().split()))
ans=-float("inf")
for i in range(N):
   st=i
   tmp=0
   for j in range(K):
      st=P[st]
      tmp+=C[st]
      ans=max(ans,tmp)
      if i==st:
         break
   s,v=divmod(K,j+1)
   if s>1 and v==0:
      s-=1
   tmp*=s
   ans=max(ans,tmp)
   for j in range(v):
      st=P[st]
      tmp+=C[st]
      ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
P=list(map(int,input().split()))
P=list(map(lambda x:x-1,P)) 
C=list(map(int,input().split()))
ans=-float("inf")
for i in range(N):
   st=i
   tmp=0
   maximum=-float("inf")
   for j in range(K):
      st=P[st]
      tmp+=C[st]
      maximum=max(maximum,tmp)
      if i==st:
         break
   s,v=divmod(K,j+1)
   if tmp>0:
      maximum+=s*tmp
      if v==0:
         maximum-=tmp
   ans=max(ans,maximum)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
P = list(map(int, input().split()))
P = list(map(lambda x: x-1, P))
C = list(map(int, input().split()))

result_max = -10**14

for i in range(N):
    tmp = P[i]
    cumulative_cost = [C[tmp]]
    while True:
        tmp = P[tmp]
        tmp_cum_cost = cumulative_cost[len(cumulative_cost)-1] + C[tmp]
        cumulative_cost.append(tmp_cum_cost)
        if tmp == i:
            break

    one_loop_sum = cumulative_cost[len(cumulative_cost)-1]
    for j, x in enumerate(cumulative_cost):
        if one_loop_sum <= 0:
            result_max = max(result_max, x)
        else:
            result_max = max(result_max, x + one_loop_sum * (K - j - 1) // len(cumulative_cost))

    # if len(cumulative_cost) < K:
    #     one_loop_sum = cumulative_cost[len(cumulative_cost)-1]
    #     tmp_sum = 0
    #     if one_loop_sum > 0:
    #         tmp_sum += (K // len(cumulative_cost)) * one_loop_sum
    #         rem_K = K % len(cumulative_cost)
    #         if rem_K > 0:
    #             tmp_sum += max(cumulative_cost[0:rem_K])
    #     else:
    #         tmp_sum = max(cumulative_cost)
    #     result_max = max(result_max, tmp_sum)
    # else:
    #     result_max = max(result_max, max(cumulative_cost[0:K]))

print(result_max)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
P = list(map(int, input().split()))
P = list(map(lambda x: x-1, P))
C = list(map(int, input().split()))

result_max = -10**14

for i in range(N):
    tmp = P[i]
    cumulative_cost = [C[tmp]]
    while True:
        tmp = P[tmp]
        tmp_cum_cost = cumulative_cost[len(cumulative_cost)-1] + C[tmp]
        cumulative_cost.append(tmp_cum_cost)
        if tmp == i:
            break

    if len(cumulative_cost) <= K:
        one_loop_sum = cumulative_cost[len(cumulative_cost)-1]
        tmp_sum = 0
        if one_loop_sum > 0:
            tmp_sum += (K // len(cumulative_cost)) * one_loop_sum
            rem_K = K % len(cumulative_cost)
            if rem_K > 0:
                tmp_sum += max(cumulative_cost[0:rem_K])
        else:
            tmp_sum = max(cumulative_cost)
        result_max = max(result_max, tmp_sum)
    else:
        result_max = max(result_max, max(cumulative_cost[0:K]))

print(result_max)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
P = list(map(int, input().split()))
P = list(map(lambda x: x-1, P))
C = list(map(int, input().split()))

result_max = -10**14

for i in range(N):
    tmp = P[i]
    cumulative_cost = [C[tmp]]
    while True:
        tmp = P[tmp]
        tmp_cum_cost = cumulative_cost[len(cumulative_cost)-1] + C[tmp]
        cumulative_cost.append(tmp_cum_cost)
        if tmp == i:
            break

    if len(cumulative_cost) < K:
        one_loop_sum = cumulative_cost[len(cumulative_cost)-1]
        tmp_sum = 0
        if one_loop_sum >= 0:
            tmp_sum += (K // len(cumulative_cost)) * one_loop_sum
            rem_K = K % len(cumulative_cost)
            if rem_K > 0:
                tmp_sum += max(cumulative_cost[0:rem_K])
        else:
            tmp_sum = max(cumulative_cost)
        result_max = max(result_max, tmp_sum)
    else:
        result_max = max(result_max, max(cumulative_cost[0:K]))

print(result_max)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
P = list(map(int, input().split()))
P = list(map(lambda x: x-1, P))
C = list(map(int, input().split()))

result_max = -10**14

for i in range(N):
    tmp = P[i]
    cumulative_cost = [C[tmp]]
    while True:
        tmp = P[tmp]
        tmp_cum_cost = cumulative_cost[len(cumulative_cost)-1] + C[tmp]
        cumulative_cost.append(tmp_cum_cost)
        if tmp == i:
            break

    if len(cumulative_cost) < K:
        one_loop_sum = cumulative_cost[len(cumulative_cost)-1]
        tmp_sum = 0
        if one_loop_sum > 0:
            tmp_sum += (K // len(cumulative_cost)) * one_loop_sum
            rem_K = K % len(cumulative_cost)
            if rem_K > 0:
                tmp_sum += max(cumulative_cost[0:rem_K])
        else:
            tmp_sum = max(cumulative_cost)
        result_max = max(result_max, tmp_sum)
    else:
        result_max = max(result_max, max(cumulative_cost[0:K]))

print(result_max)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
plst, clst = [], []
plst = list(map(int, input().split()))
clst = list(map(int, input().split()))

ans = float('-inf')
visited = [0 for _ in range(n)]
for _ in range(n):
    new = plst[_] - 1
    lst = []
    i = 0
    temp = 0
    while visited[new] == 0:
        temp += clst[new]
        visited[new] = 1
        new = plst[new] -1
        i += 1
        lst.append(new)
    if lst:
        mod = k % i
        loop = k // i
        temp2 = temp * loop
        if mod == 0: #ループが割り切れる場合
            ans = max(ans, temp2)
        for l in lst:
            new = l
            temp = 0
            newi = 0
            while newi < min(k, i):
                temp += clst[new]
                if newi < mod:
                    ans = max(ans, temp2 + temp)
                ans = max(ans, temp)
                new = plst[new] -1
                newi += 1  
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
plst, clst = [], []
plst = list(map(int, input().split()))
clst = list(map(int, input().split()))

ans = float('-inf')
visited = [0 for _ in range(n)]
for _ in range(n):
    new = plst[_] - 1
    lst = []
    i = 0
    temp = 0
    while visited[new] == 0:
        temp += clst[new]
        visited[new] = 1
        new = plst[new] -1
        i += 1
        lst.append(new)
    if lst:
        mod = k % i
        loop = k // i
        temp2 = temp * loop
        if k % i == 0: #ループが割り切れる場合
            ans = max(ans, temp2)
        for l in lst:
            new = l
            temp = 0
            newi = 0
            while newi < min(k, i):
                temp += clst[new]
                if newi < mod:
                    ans = max(ans, temp2 + temp)
                ans = max(ans, temp)
                new = plst[new] -1
                newi += 1  
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
plst, clst = [], []
plst = list(map(int, input().split()))
clst = list(map(int, input().split()))

ans = float('-inf')
visited = [0 for _ in range(n)]
for _ in range(n):
    new = plst[_] - 1
    lst = []
    i = 0
    temp = 0
    while visited[new] == 0:
        temp += clst[new]
        visited[new] = 1
        new = plst[new] -1
        i += 1
        lst.append(new)
    if lst:
        mod = k % i
        loop = k // i
        temp2 = temp * loop
        ans = max(ans, temp2)
        for l in lst:
            new = l
            temp = 0
            newi = 0
            while newi < min(k, i):
                temp += clst[new]
                if newi < mod:
                    ans = max(ans, temp2 + temp)
                ans = max(ans, temp)
                new = plst[new] -1
                newi += 1  
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
plst, clst = [], []
plst = list(map(int, input().split()))
clst = list(map(int, input().split()))


ans = float('-inf')
visited = [0 for _ in range(n)]
for _ in range(n):
    new = plst[_] - 1
    lst = []
    i = 0
    temp = 0
    while visited[new] == 0:
        temp += clst[new]
        visited[new] = 1
        new = plst[new] -1
        i += 1
        lst.append(new)
    if lst:
        mod = k % i
        ans = max(ans, temp)
        loop = k // i
        temp2 = temp * loop
        for l in lst:
            new = l
            temp = 0
            newi = 0
            while newi < min(k, i):
                temp += clst[new]
                if newi < mod:
                    ans = max(ans, temp2 + temp)
                ans = max(ans, temp)
                new = plst[new] -1
                newi += 1  
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
plst, clst = [], []
plst = list(map(int, input().split()))
clst = list(map(int, input().split()))

ans = float('-inf')
visited = [0 for _ in range(n)]
for _ in range(n):
    new = plst[_] - 1
    lst = []
    i = 0
    temp = 0
    while visited[new] == 0:
        temp += clst[new]
        visited[new] = 1
        new = plst[new] -1
        i += 1
        lst.append(new)
    if lst:
        mod = k % i
        loop = k // i
        temp2 = temp * loop
        ans = max(ans, temp2)
        for l in lst:
            new = l
            temp = 0
            newi = 0
            while newi < min(k, i):
                temp += clst[new]
                new = plst[new] -1
                if newi < mod:
                    ans = max(ans, temp2 + temp)
                ans = max(ans, temp)
                newi += 1  
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
plst, clst = [], []
plst = list(map(int, input().split()))
clst = list(map(int, input().split()))

ans = float('-inf')
visited = [0 for _ in range(n)]
for _ in range(n):
    new = plst[_] - 1
    lst = []
    i = 0
    temp = 0
    while visited[new] == 0:
        temp += clst[new]
        visited[new] = 1
        new = plst[new] -1
        i += 1
        lst.append(new)
    if lst:
        mod = k % i
        loop = k // i
        temp2 = max(temp * loop, 0)
        ans = max(ans, temp2)
        for l in lst:
            new = l
            temp = 0
            newi = 0
            while newi < min(k, i):
                temp += clst[new]
                new = plst[new] -1
                if newi < mod:
                    ans = max(ans, temp2 + temp)
                ans = max(ans, temp)
                newi += 1  
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
plst, clst = [], []
plst = list(map(int, input().split()))
clst = list(map(int, input().split()))

ans = float('-inf')
visited = [0 for _ in range(n)]
for _ in range(n):
    new = plst[_] - 1
    lst = []
    i = 0
    temp = 0
    while visited[new] == 0:
        temp += clst[new]
        visited[new] = 1
        new = plst[new] -1
        i += 1
        lst.append(new)
    if lst:
        mod = k % i
        loop = k // i
        temp2 = max(temp * loop, 0)    
        for l in lst:
            new = l
            temp = 0
            newi = 0
            while newi < min(k, i):
                temp += clst[new]
                new = plst[new] -1
                if newi < mod:
                    ans = max(ans, temp2 + temp)
                ans = max(ans, temp)
                newi += 1  
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
plst, clst = [], []
plst = list(map(int, input().split()))
clst = list(map(int, input().split()))

ans = float('-inf')
visited = [0 for _ in range(n)]
for _ in range(n):
    new = plst[_] - 1
    lst = []
    i = 0
    temp = 0
    while visited[new] == 0:
        temp += clst[new]
        visited[new] = 1
        new = plst[new] -1
        i += 1
        lst.append(new)
    if lst:
        mod = k % i
        loop = k // i
        temp2 = max(temp * loop, 0)    
        for l in lst:
            new = l
            temp = 0
            newi = 0
            while newi < i:
                temp += clst[new]
                new = plst[new] -1
                if newi < mod:
                    ans = max(ans, temp2 + temp)
                ans = max(ans, temp)
                newi += 1  
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
P = list(map(int, input().split()))
P = list(map(lambda x: x-1, P))
C = list(map(int, input().split()))

result_max = -10**14

for i in range(N):
    tmp = P[i]
    cumulative_cost = [C[tmp]]
    while True:
        tmp = P[tmp]
        tmp_cum_cost = cumulative_cost[len(cumulative_cost)-1] + C[tmp]
        cumulative_cost.append(tmp_cum_cost)
        if tmp == i:
            break

    if len(cumulative_cost) < K:
        one_loop_sum = cumulative_cost[len(cumulative_cost)-1]
        tmp_sum = 0
        if one_loop_sum > 0:
            tmp_sum += (K // len(cumulative_cost)) * one_loop_sum
            rem_K = K % len(cumulative_cost)
            if rem_K > 0:
                tmp_sum += max(cumulative_cost[0:rem_K])
        else:
            tmp_sum = max(cumulative_cost)

        result_max = max(result_max, tmp_sum)
    else:
        result_max = max(result_max, max(cumulative_cost[0:K]))

print(result_max)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
plst, clst = [], []
plst = list(map(int, input().split()))
clst = list(map(int, input().split()))

ans = float('-inf')
visited = [0 for _ in range(n)]
for _ in range(n):
    new = plst[_] - 1
    if visited[new] > 0:
      ans = max(ans, clst[new])
      continue
    lst = []
    i = 0
    temp = 0
    while i < k:
        if visited[new] > 0:
            break
        temp += clst[new]
        visited[new] = 1
        ans = max(ans, temp)
        new = plst[new] -1
        i += 1
        lst.append(new)
    if temp <= 0: #一周の合計が0以下ならループしても意味ないので
        continue 
    else:
        mod = k % i
        temp2 = temp * (k // i)
        for l in lst:
            new = l
            temp = 0
            newi = 0
            while newi < mod:
                temp += clst[new]
                new = plst[new] -1
                ans = max(ans, temp2 + temp)    
                newi += 1  
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
P = list(map(int, input().split()))
P = list(map(lambda x: x-1, P))
C = list(map(int, input().split()))

result_max = -10**14

for i in range(N):
    tmp = P[i]
    cumulative_cost = [C[tmp]]
    while True:
        tmp = P[tmp]
        tmp_cum_cost = cumulative_cost[len(cumulative_cost)-1] + C[tmp]
        cumulative_cost.append(tmp_cum_cost)
        if tmp == i:
            break

    if len(cumulative_cost) < K:
        one_loop_sum = cumulative_cost[len(cumulative_cost)-1]
        tmp_sum = 0
        if one_loop_sum > 0:
            tmp_sum += (K // len(cumulative_cost)) * one_loop_sum
            rem_K = K % len(cumulative_cost)
            if rem_K != 0:
                tmp_sum += max(cumulative_cost[0:rem_K])
        else:
            tmp_sum = max(cumulative_cost)

        result_max = max(result_max, tmp_sum)
    else:
        result_max = max(result_max, max(cumulative_cost[0:K]))

print(result_max)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
plst, clst = [], []
plst = list(map(int, input().split()))
clst = list(map(int, input().split()))

ans = float('-inf')
visited = [0 for _ in range(n)]
for _ in range(n):
    new = plst[_] - 1
    if visited[new] > 0:
        continue
    lst = []
    i = 0
    temp = 0
    while i < k:
        if visited[new] > 0:
            break
        temp += clst[new]
        visited[new] = 1
        ans = max(ans, temp)
        new = plst[new] -1
        i += 1
        lst.append(new)
    if temp <= 0: #一周の合計が0以下ならループしても意味ないので
        continue 
    else:
        mod = k % i
        temp2 = temp * (k // i)
        for l in lst:
            new = l
            temp = 0
            newi = 0
            while newi < mod:
                temp += clst[new]
                new = plst[new] -1
                ans = max(ans, temp2 + temp)    
                newi += 1  
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
plst, clst = [], []
plst = list(map(int, input().split()))
clst = list(map(int, input().split()))

i = 0
ans = float('-inf')
visited = [0 for _ in range(n)]
for _ in range(n):
    i = 0
    temp = 0
    new = plst[_] - 1
    visited = [0 for _ in range(n)]
    while i < k:
        if visited[new] > 0:
            if temp <= 0: #一周の合計が0以下ならループしても意味ないので
                break
            else:
                mod = k % i
                newi = 0
                new = _
                temp *= (k // i)
                while newi < mod:
                    temp += clst[new]
                    ans = max(ans, temp)
                    new = plst[new] -1
                    newi += 1
                break
        else:
            temp += clst[new]
            visited[new] = 1
            ans = max(ans, temp)
            new = plst[new] -1
            i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math
import copy
import random
import itertools
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = 10 ** 21
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def solve(n=None, s1=None, s2=None):
    n, k = getList()
    pli = getList()
    cli = getList()

    ans = max(cli)
    for i in range(n):
        cycle = [i]
        pts = [cli[i]]
        pos = i
        while True:
            pos = pli[pos] - 1
            if pos != i:
                cycle.append(pos)
                pts.append(cli[pos])
            else:
                break
        lc = len(cycle)
        for j in range(lc - 1):
            pts[j+1] += pts[j]

        if k <= lc:
            tmp = max(pts[:k])
        else:
            each = k % lc
            rotate = k // lc
            if pts[-1] > 0:
                tmp = pts[-1] * rotate
                if each:
                    tmp += max(pts[:each])
            else:
                tmp = max(pts)
        # print(tmp, sum(pts), pts)
        ans = max(ans, tmp)

    print(ans)

def main():
    n = getN()
    for _ in range(n):
        solve()
    return


if __name__ == "__main__":
    # main()
    solve()

Submission status: Wrong Answer

Solution code:
import sys
import math
import copy
import random
import itertools
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = 10 ** 21
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def solve(n=None, s1=None, s2=None):
    n, k = getList()
    pli = getList()
    cli = getList()

    each = n % k
    cycles = n // k

    ans = -INF
    for i in range(n):
        pos = i
        score = 0
        for j in range(k):
            pos = pli[pos] - 1
            score += cli[pos]
            ans = max(ans, score)

    if sum(cli) > 0:
        ans += sum(cli) * cycles

    print(ans)
def main():
    n = getN()
    for _ in range(n):
        solve()
    return


if __name__ == "__main__":
    # main()
    solve()

Submission status: Wrong Answer

Solution code:
import sys
import math
import copy
import random
import itertools
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = 10 ** 21
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def solve(n=None, s1=None, s2=None):
    n, k = getList()
    pli = getList()
    cli = getList()

    each = n % k
    cycles = n // k

    ans = -INF
    for i in range(n):
        pos = i
        score = 0
        for j in range(k):
            pos = pli[pos-1]
            score += cli[pos-1]
            ans = max(ans, score)

    if sum(cli) > 0:
        ans += sum(cli) * cycles

    print(ans)
def main():
    n = getN()
    for _ in range(n):
        solve()
    return


if __name__ == "__main__":
    # main()
    solve()

Submission status: Wrong Answer

Solution code:
import sys
import math
import copy
import random
import itertools
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def solve(n=None, s1=None, s2=None):
    n, k = getList()
    pli = getList()
    cli = getList()

    each = n % k
    cycles = n // k

    ans = -INF
    for i in range(n):
        pos = i
        score = 0
        for j in range(k):
            pos = pli[pos-1]
            score += cli[pos-1]
            ans = max(ans, score)

    if sum(cli) > 0:
        ans += sum(cli) * cycles

    print(ans)
def main():
    n = getN()
    for _ in range(n):
        solve()
    return


if __name__ == "__main__":
    # main()
    solve()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
moves = [0] + list(map(int, input().split()))
points = [0] + list(map(int, input().split()))
INF = 10**10

answer = -INF
for start in range(1, N+1):
  circle = [start]
  next = moves[start]
  c_score = points[start]
  while(next != start):
    circle.append(next)
    c_score += points[next]
    next = moves[next]
  m_score = 0
  for i in range(len(circle)):
    m_score += points[circle[i]]
    loop_cnt = (K-i-1)//len(circle)
    answer = max(answer, m_score + max(0, loop_cnt * c_score))
print(answer)

Submission status: Wrong Answer

Solution code:
N,K = list(map(int,input().split()))
P = list(map(int,input().split()))
C = list(map(int,input().split()))

Loops = []
Done = [0]*N
for i in range(N):
    if Done[i]==1:
        continue
    Loop = [C[i]]
    Done[i] = 1
    next = P[i]-1
    while Done[next]==0:
        Loop.append(C[next])
        Done[next] = 1
        next = P[next]-1
    Loops.append(Loop)

out = -1*10**10
for Loop in Loops:
    cands = []
    S = sum(Loop)
    LN = len(Loop)
    LoLo = Loop + Loop
    for l in range(LN):
        now = S
        for r in range(LN):
            x,y = l,l+LN-1-r
            now = now-LoLo[y]
            cands.append([now,y-x])
    if S>0:
        point = (K//LN)*S
        if K%LN!=0:
            maxpoint = 0
            amari = K - (K//LN)*LN
            for cand in cands:
                if cand[1]<=amari:
                    maxpoint = max(maxpoint,cand[0])
            point = point + maxpoint
    else:
        point = max(Loop)
        maxpoint = max(Loop)
        amari = K
        for cand in cands:
            if 0<cand[1]<=amari:
                maxpoint = max(maxpoint,cand[0])
        point = maxpoint
    out = max(point,out)
print(out)