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:
n,k=map(int,input().split())

p=list(map(int,input().split()))
score=list(map(int,input().split()))

group_score=[]
checked=[0]*n

for i in range(n):
    if checked[i]==0:
        tmp_group=[]
        tmp=i
        while checked[tmp]==0:
            checked[tmp]=1
            tmp_group.append(score[tmp])
            tmp=p[tmp]-1
        group_score.append(tmp_group)



#aryの中から連続するk個選択するときの最大
def calc(k):
    global group
    ans=group[0]
    for i in range(len(group)):
        tmp=0
        for j in range(k):
            tmp+=group[(i+j)%len(group)]
            if ans<tmp:
                ans=tmp
    return ans

ans=score[0]
for group in group_score:
    #一周以上する場合
    if len(group)<k:
        cycle=sum(group)
        if cycle>0:
            cycle_sum=cycle*(k//len(group))
            tmp=calc(k%len(group))
            ans=max(ans,cycle_sum+tmp)
            tmp=calc(k)
            ans=max(ans,tmp)

        else:
            tmp=calc(len(group))
            ans=max(ans,tmp)

    #一周しない場合k)
    else:
        tmp=calc(k)
        ans=max(ans,tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))

ans=-float('inf')
for i in range(n):
  T=1
  index=p[i]-1
  score=c[index]
  flag=True
  while flag:
    T+=1
    index=p[index]-1
    score+=c[index]
    if index==p[i]-1:
      flag=False
  #場合分け開始
  if T>=k:
    score_list=[c[p[i]-1]]+[0]*(k-1)
    index=p[i]-1
    for s in range(1,k):
      index=p[index]-1
      score_list[s]=score_list[s-1]+c[index]
    score=max(score_list)
  else:
    if score<=0:
      score_list=[c[p[i]-1]]+[0]*(T-1)
      index=p[i]-1
      for s in range(1,T):       
        index=p[index]-1
        score_list[s]=score_list[s-1]+c[index]        
      score=max(score_list)
    else:
      m1=k//T
      m2=k%T
      if m2==0:
        score=score*m1
      else:
        score_list=[c[p[i]-1]]+[0]*(m2-1)
        index=p[i]-1
        for s in range(1,m2):
          index=p[index]-1
          score_list[s]=score_list[s-1]+c[index]
        score=max(score_list)+score*m1
  ans=max(ans,score)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())

p=list(map(int,input().split()))
score=list(map(int,input().split()))

group_score=[]
checked=[0]*n

for i in range(n):
    if checked[i]==0:
        tmp_group=[]
        tmp=i
        while checked[tmp]==0:
            checked[tmp]=1
            tmp_group.append(score[tmp])
            tmp=p[tmp]-1
        group_score.append(tmp_group)



#aryの中から連続するk個選択するときの最大
def calc(k):
    global group
    ans=group[0]
    for i in range(len(group)):
        tmp=0
        for j in range(k):
            tmp+=group[(i+j)%len(group)]
            if ans<tmp:
                ans=tmp
    return ans

ans=score[0]
for group in group_score:
    #一周以上する場合
    if len(group)>k:
        cycle=sum(group)
        if cycle>0:
            cycle_sum=cycle*(len(group)//k)
            tmp=calc(len(group)%k)
            ans=max(ans,cycle_sum+tmp)
            tmp=calc(k)
            ans=max(ans,tmp)

        else:
            tmp=calc(len(group))
            ans=max(ans,tmp)

    #一周しない場合k)
    else:
        tmp=calc(k)
        ans=max(ans,tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
score = [0 for _ in range(n)]
loopn = [0 for _ in range(n)]
done = {i: True for i in range(n)}
cycles = []
nux = -100000000
cycles_score = []
for start in range(n):
    if done[start]:
        done[start] = False
        cycle = [start]
        score = C[start]
        nex = P[start]-1
        while nex != start:
            done[nex] = False
            cycle.append(nex)
            score += C[nex]
            nex = P[nex]-1
        cycles.append(cycle)
        cycles_score.append(score)

for score, cycle in zip(cycles_score, cycles):
    l = len(cycle)
    acum = [0 for _ in range(2*l+1)]
    for i in range(2*l):
        acum[i+1] = acum[i]+C[cycle[i % l]]
    if score < 0:
        for r in range(l+1):
            for i in range(l):
                if r == 0:
                    continue
                if r <= k:
                    nux = max(nux, acum[i+r]-acum[i])
    else:
        for r in range(l+1):
            q = (k-r)//l
            for i in range(l):
                if r == 0 and q == 0:
                    continue
                if q <= 0 and r > k:
                    continue
                nux = max(nux, score*q+acum[i+r]-acum[i])


print(nux)

"""n, k = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

score = [0 for _ in range(n)]
loopn = [0 for _ in range(n)]
done = {i: True for i in range(n)}
for start in range(n):
    if done[start]:
        nex = -1
        j = 0
        tuuka = []
        nax = -float("inf")
        while nex != start:
            if nex == -1:
                nex = P[start]-1
            else:
                nex = P[nex]-1
            tuuka.append(nex)
            score[start] += C[nex]
            nax = max(nax, score[start])
            j += 1
        loopn[start] = j
        for i in tuuka:
            done[i] = False
            loopn[i] = j
            score[i] = score[start]
            nokori = k-(k//loopn[start])*loopn[start]

    if score[start] < 0:
        score[start] = 0
    else:
        score[start] = (k//loopn[start])*score[start]
        nokori = k-(k//loopn[start])*loopn[start]
        i = 0
        nnnnn = (k//loopn[start])
        nscore = 0
        nax = 0
        while i < nokori:
            nex = P[nex]-1
            nscore += C[nex]
            if nscore < 0:
                break
            nax = max(nax, nscore)
            i += 1
        if nax > 0:
            score[start] += nax


ans = max(score)
if ans <= 0:
    ans = max(C)
print(ans)
"""

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())

p=list(map(int,input().split()))
score=list(map(int,input().split()))

group_score=[]
checked=[0]*n

for i in range(n):
    if checked[i]==0:
        tmp_group=[]
        tmp=i
        while checked[tmp]==0:
            checked[tmp]=1
            tmp_group.append(score[tmp])
            tmp=p[tmp]-1
        group_score.append(tmp_group)



#aryの中から連続するk個選択するときの最大
def calc(k):
    global group
    ans=group[0]
    for i in range(len(group)):
        tmp=0
        for j in range(k):
            tmp+=group[(i+j)%len(group)]
            ans=max(ans,tmp)

    return ans

ans=score[0]
for group in group_score:
    #一周以上する場合
    if len(group)>k:
        cycle=sum(group)
        if cycle>0:
            cycle_sum=cycle*(len(group)//k)
            tmp=calc(len(group)%k)
            ans=max(ans,cycle_sum+tmp)
            tmp=calc(k)
            ans=max(ans,tmp)

        else:
            tmp=calc(len(group))
            ans=max(ans,tmp)

    #一周しない場合k)
    else:
        tmp=calc(k)
        ans=max(ans,tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())

p=list(map(int,input().split()))
c=list(map(int,input().split()))

check=[0]*n
group=[]

for i in range(n):
    tmp=i
    if check[tmp]==0:
        tmp_group=[]
        while check[tmp]==0:
            tmp_group.append(tmp)
            check[tmp]=1
            tmp=p[tmp]-1

        group.append(tmp_group)

ans="start"

#startの最低とgoalの最高の和
for item in group:
    
    #一周すると何になるか。これはどこスタートでも同じ
    circle=0
    cnt=k
    if len(item)<k:
        for v in item:
            circle+=c[v]
        circle=circle*(k//len(item))
        cnt=k%len(item)
    
    #一周した後のあまりで最強を探す

    goal=(-1)*(10**18)
    
    #startは各groupのindexが入ってる
    for start in range(len(item)):
        tmp=0
        #print("start",item,start,cnt)
        for i in range(start,start+cnt):
            #print(">>",i)
            tmp+=c[item[i%len(item)]]
            #print(item[i%len(item)],c[item[i%len(item)]])
            goal=max(goal,tmp)

    #print(item,circle,goal)
    if ans=="start":
        ans=goal+max(0,circle)
    else:
        ans=max(ans,goal+max(0,circle))

    if len(item)<=k:
        for i in range(len(item)):
            tmp_ans=c[item[i]]
            for j in range(1,len(item)):
                tmp_ans+=c[item[(i+j)%len(item)]]
                ans=max(ans,tmp_ans)




print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
score = [0 for _ in range(n)]
loopn = [0 for _ in range(n)]
done = {i: True for i in range(n)}
cycles = []
nux = -100000000
cycles_score = []
for start in range(n):
    if done[start]:
        done[start] = False
        cycle = [start]
        score = C[start]
        nex = P[start]-1
        while nex != start:
            done[nex] = False
            cycle.append(nex)
            score += C[nex]
            nex = P[nex]-1
        cycles.append(cycle)
        cycles_score.append(score)

for score, cycle in zip(cycles_score, cycles):
    l = len(cycle)
    acum = [0 for _ in range(2*l+1)]
    acum[0] = 0
    for i in range(2*l):
        acum[i+1] = acum[i]+C[cycle[i % l]]
    if score < 0:
        for r in range(l):
            for i in range(l):
                if r == 0:
                    continue
                if r <= k:
                    nux = max(nux, acum[i+r]-acum[i])
    else:
        for r in range(l):
            q = (k-r)//l
            for i in range(l):
                if r == 0 and q == 0:
                    continue
                if q == 0 and r > k:
                    continue
                nux = max(nux, score*q+acum[i+r]-acum[i])


print(nux)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
score = [0 for _ in range(n)]
loopn = [0 for _ in range(n)]
done = {i: True for i in range(n)}
cycles = []
nux = -100000000
cycles_score = []
for start in range(n):
    if done[start]:
        done[start] = False
        cycle = [start]
        score = C[start]
        nex = P[start]-1
        while nex != start:
            done[nex] = False
            cycle.append(nex)
            score += C[nex]
            nex = P[nex]-1
        cycles.append(cycle)
        cycles_score.append(score)

for score, cycle in zip(cycles_score, cycles):
    l = len(cycle)
    acum = [0 for _ in range(2*l+1)]
    acum[0] = 0
    for i in range(2*l):
        acum[i+1] = acum[i]+C[cycle[i % l]]
    if score < 0:
        for r in range(l):
            for i in range(l):
                if r == 0:
                    continue
                if r <= k:
                    nux = max(nux, acum[i+r]-acum[i])
    else:
        for r in range(l):
            q = (k-r)//l
            for i in range(l):
                if r == 0 and q == 0:
                    continue
                if q == 0 and r > k:
                    continue
                nux = max(nux, score*q+acum[i+r]-acum[i])


print(nux)

"""n, k = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

score = [0 for _ in range(n)]
loopn = [0 for _ in range(n)]
done = {i: True for i in range(n)}
for start in range(n):
    if done[start]:
        nex = -1
        j = 0
        tuuka = []
        nax = -float("inf")
        while nex != start:
            if nex == -1:
                nex = P[start]-1
            else:
                nex = P[nex]-1
            tuuka.append(nex)
            score[start] += C[nex]
            nax = max(nax, score[start])
            j += 1
        loopn[start] = j
        for i in tuuka:
            done[i] = False
            loopn[i] = j
            score[i] = score[start]
            nokori = k-(k//loopn[start])*loopn[start]

    if score[start] < 0:
        score[start] = 0
    else:
        score[start] = (k//loopn[start])*score[start]
        nokori = k-(k//loopn[start])*loopn[start]
        i = 0
        nnnnn = (k//loopn[start])
        nscore = 0
        nax = 0
        while i < nokori:
            nex = P[nex]-1
            nscore += C[nex]
            if nscore < 0:
                break
            nax = max(nax, nscore)
            i += 1
        if nax > 0:
            score[start] += nax


ans = max(score)
if ans <= 0:
    ans = max(C)
print(ans)
"""

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))
K = K-1
ans = -10**9-1
for i in range(N):
   start = i
   circle = 0
   number = 0
   while i != start:
      circle += c[i-1]
      number += 1
      i = p[i-1]
   number += 1
   circle += c[i-1]
   for j in range (min(number, K+1)):
      sum = 0
      for k in range(i, i+j+1):
         sum += c[k-1]
      if circle > 0:
         sum = circle*((K-j)//number) + sum
      ans = max(sum, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))
K = K-1
ans = -10**9-1
for i in range(N):
   start = i
   circle = 0
   number = 0
   while i != start:
      circle += c[i-1]
      number += 1
      i = p[i-1]
   number += 1
   circle += c[i-1]
   for j in range (min(number, K+1)):
      sum = 0
      for k in range(i, i+j+1):
         sum += c[k-1]
      if circle > 0:
         sum = circle*(K//number) + sum
      ans = max(sum, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))
K = K-1
ans = -10**9-1
for i in range(N):
   start = i
   circle = 0
   number = 0
   while i != start:
      circle += c[i-1]
      number += 1
      i = p[i-1]
   number += 1
   circle += c[i-1]
   for j in range (min(number, K)):
      sum = 0
      for k in range(i, i+j+1):
         sum += c[k-1]
      if circle > 0:
         sum = circle*(K//number) + sum
      ans = max(sum, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
G = [-1 for i in range(N+1)]

for i, v in enumerate(P):
    G[i+1] = (v, C[v-1])

ans = -float("inf")

for i in range(1, N+1):
    loop = []
    S = i
    visited = [False for i in range(N+1)]
    while visited[S] == False:
        loop.append(G[S][-1])
        visited[S] = True
        S = G[S][0]

    circle_sum = sum(loop)
    this_case_ans = -float("inf")

    if circle_sum < 0:
        pass_sum = 0
        for i in range(len(loop)):
            pass_sum += loop[i]
            this_case_ans = max(this_case_ans, pass_sum)
        ans = max(ans, this_case_ans)
        continue

    else:
        L = len(loop)
        for i in range(1,L):
            loop[i] += loop[i-1]
        loop.insert(0, -float("inf"))
        for i in range(1, L+1):
            loop[i] = max(loop[i], loop[i-1])
        loop[0] = 0
        if K//L > 0:
            this_case_ans = max(K//L * circle_sum +
                                loop[K % L], (K//L-1)*circle_sum + loop[-1])
        else:
            this_case_ans = max(this_case_ans, loop[K+1])
        ans = max(ans, this_case_ans)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))

ans=-float('inf')
for i in range(n):
  T=1
  index=p[i]-1
  score=c[index]
  flag=True
  while flag:
    T+=1
    index=p[index]-1
    score+=c[index]
    if index==p[i]-1:
      flag=False
  #場合分け開始
  if T>=k:
    score_list=[c[p[i]-1]]+[0]*(k-1)
    index=p[i]-1
    for s in range(1,k):
      index=p[index]-1
      score_list[s]=score_list[s-1]+c[index]
    score=max(score_list)
  else:
    if score<=0:
      score_list=[c[p[i]-1]]+[0]*(n-1)
      index=p[i]-1
      for s in range(1,n):       
        index=p[index]-1
        score_list[s]=score_list[s-1]+c[index]        
      score=max(score_list)
    else:
      m1=k//T
      m2=k%T
      if m2==0:
        score=score*m1
      else:
        score_list=[c[p[i]-1]]+[0]*(m2-1)
        index=p[i]-1
        for s in range(1,m2):
          index=p[index]-1
          score_list[s]=score_list[s-1]+c[index]
        score=max(score_list)+score*m1
  ans=max(ans,score)

print(ans)

Submission status: Wrong Answer

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

P = list(map(int, input().split()))
C = list(map(int, input().split()))

P = [0] + P
C = [0] + C
Loop = []

search = list(range(1, N+1))

while len(search) != 0:
    num = search.pop()
    loop = [num]
    while len(loop) <= N:
        num = P[num]
        if num == loop[0]:
            break
        loop.append(num)
        search.remove(num)
    Loop.append(loop)

#print(Loop)

Sam = []
Loop_num = []
for loop in Loop:
    sam = 0
    loop_num = 0
    for n in loop:
        sam += C[n]
        loop_num += 1
    Loop_num.append(loop_num)
    Sam.append(sam)

Max = -10000000000
for i in range(len(Loop)):
    #print('Loop[{}]'.format(i))
    score = 0
    kaiten = K // Loop_num[i]
    #print('kaiten:{}'.format(kaiten))
    amari = K % Loop_num[i]
    #print('amari:{}'.format(amari))
    if Sam[i] > 0:
        score += kaiten * Sam[i]
        #print('score += {}'.format(kaiten * Sam[i]))
    score2max = -10000000000
    for num in range(Loop_num[i]):
        score2 = 0
        if amari == 0:
            amari = Loop_num[i]
        for n in range(amari):
            idn = (num + n) % Loop_num[i]
            score2 += C[Loop[i][idn]]
            if score2 > score2max:
                score2max = score2
                #print('score2 appdate: {}'.format(score2max))
    score += score2max
    #print('score: {}'.format(score))
    if score > Max:
        Max = score
        #print('score apdate')
print(Max)

Submission status: Wrong Answer

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

P = list(map(int, input().split()))
C = list(map(int, input().split()))

P = [0] + P
C = [0] + C
Loop = []

search = list(range(1, N+1))

while len(search) != 0:
    num = search.pop()
    loop = [num]
    while len(loop) <= N:
        num = P[num]
        if num == loop[0]:
            break
        loop.append(num)
        search.remove(num)
    Loop.append(loop)

#print(Loop)

Sam = []
Loop_num = []
for loop in Loop:
    sam = 0
    loop_num = 0
    for n in loop:
        sam += C[n]
        loop_num += 1
    Loop_num.append(loop_num)
    Sam.append(sam)

Max = -1000000000000000000
for i in range(len(Loop)):
    #print('Loop[{}]'.format(i))
    score = 0
    kaiten = K // Loop_num[i]
    #print('kaiten:{}'.format(kaiten))
    amari = K % Loop_num[i]
    #print('amari:{}'.format(amari))
    if Sam[i] > 0:
        score += kaiten * Sam[i]
        #print('score += {}'.format(kaiten * Sam[i]))
    score2max = -1000000000000000000
    for num in range(Loop_num[i]):
        score2 = 0
        if amari == 0:
            amari = Loop_num[i]
        for n in range(amari):
            idn = (num + n) % Loop_num[i]
            score2 += C[Loop[i][idn]]
            if score2 > score2max:
                score2max = score2
                #print('score2 appdate: {}'.format(score2max))
    score += score2max
    #print('score: {}'.format(score))
    if score > Max:
        Max = score
        #print('score apdate')
print(Max)

Submission status: Wrong Answer

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

P = list(map(int, input().split()))
C = list(map(int, input().split()))

P = [0] + P
C = [0] + C
Loop = []

search = list(range(1, N+1))

while len(search) != 0:
    num = search.pop()
    loop = [num]
    while len(loop) <= N:
        num = P[num]
        if num == loop[0]:
            break
        loop.append(num)
        search.remove(num)
    Loop.append(loop)

#print(Loop)

Sam = []
Loop_num = []
for loop in Loop:
    sam = 0
    loop_num = 0
    for n in loop:
        sam += C[n]
        loop_num += 1
    Loop_num.append(loop_num)
    Sam.append(sam)

Max = -1000000000 * (1000000000)
for i in range(len(Loop)):
    #print('Loop[{}]'.format(i))
    score = 0
    kaiten = K // Loop_num[i]
    #print('kaiten:{}'.format(kaiten))
    amari = K % Loop_num[i]
    #print('amari:{}'.format(amari))
    if Sam[i] > 0:
        score += kaiten * Sam[i]
        #print('score += {}'.format(kaiten * Sam[i]))
    score2max = -1000000000 * (1000000000)
    for num in range(Loop_num[i]):
        score2 = 0
        if amari == 0:
            amari = Loop_num[i]
        for n in range(amari):
            idn = (num + n) % Loop_num[i]
            score2 += C[Loop[i][idn]]
            if score2 > score2max:
                score2max = score2
                #print('score2 appdate: {}'.format(score2max))
    score += score2max
    #print('score: {}'.format(score))
    if score > Max:
        Max = score
        #print('score apdate')
print(Max)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
score = [0 for _ in range(n)]
loopn = [0 for _ in range(n)]
done = {i: True for i in range(n)}
cycles = []
nux = -100000000
cycles_score = []
for start in range(n):
    if done[start]:
        done[start] = False
        cycle = [start]
        score = C[start]
        nex = P[start]-1
        while nex != start:
            done[nex] = False
            cycle.append(nex)
            score += C[nex]
            nex = P[nex]-1
        cycles.append(cycle)
        cycles_score.append(score)

for score, cycle in zip(cycles_score, cycles):
    l = len(cycle)
    acum = [0 for _ in range(2*l)]
    acum[0] = 0
    for i in range(2*l-1):
        acum[i+1] = acum[i]+C[cycle[i % l]]
    if score < 0:
        for r in range(l):
            for i in range(l+1):
                if r == 0:
                    continue
                nux = max(nux, acum[i+r]-acum[i])
    else:
        for r in range(l):
            q = (k-r)//l
            for i in range(l+1):
                if r == 0 and q == 0:
                    continue
                nux = max(nux, score*q+acum[i+r]-acum[i])


print(nux)

"""n, k = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

score = [0 for _ in range(n)]
loopn = [0 for _ in range(n)]
done = {i: True for i in range(n)}
for start in range(n):
    if done[start]:
        nex = -1
        j = 0
        tuuka = []
        nax = -float("inf")
        while nex != start:
            if nex == -1:
                nex = P[start]-1
            else:
                nex = P[nex]-1
            tuuka.append(nex)
            score[start] += C[nex]
            nax = max(nax, score[start])
            j += 1
        loopn[start] = j
        for i in tuuka:
            done[i] = False
            loopn[i] = j
            score[i] = score[start]
            nokori = k-(k//loopn[start])*loopn[start]

    if score[start] < 0:
        score[start] = 0
    else:
        score[start] = (k//loopn[start])*score[start]
        nokori = k-(k//loopn[start])*loopn[start]
        i = 0
        nnnnn = (k//loopn[start])
        nscore = 0
        nax = 0
        while i < nokori:
            nex = P[nex]-1
            nscore += C[nex]
            if nscore < 0:
                break
            nax = max(nax, nscore)
            i += 1
        if nax > 0:
            score[start] += nax


ans = max(score)
if ans <= 0:
    ans = max(C)
print(ans)
"""

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
score = [0 for _ in range(n)]
loopn = [0 for _ in range(n)]
done = {i: True for i in range(n)}
cycles = []
nux = -100000000
cycles_score = []
for start in range(n):
    if done[start]:
        done[start] = False
        cycle = [start]
        score = C[start]
        nex = P[start]-1
        while nex != start:
            done[nex] = False
            cycle.append(nex)
            score += C[nex]
            nex = P[nex]-1
        cycles.append(cycle)
        cycles_score.append(score)

for score, cycle in zip(cycles_score, cycles):
    l = len(cycle)
    acum = [0 for _ in range(2*l)]
    acum[0] = 0
    for i in range(2*l-1):
        acum[i+1] = acum[i]+C[cycle[i % l]]
    if score < 0:
        for i in range(l+1):
            for r in range(l):
                for i in range(l):
                    if r == 0:
                        continue
                    nux = max(nux, acum[i+r]-acum[i])
    else:
        for r in range(l):
            q = (k-r)//l
            for i in range(l):
                if r == 0 and q == 0:
                    continue
                nux = max(nux, score*q+acum[i+r]-acum[i])


print(nux)

"""n, k = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

score = [0 for _ in range(n)]
loopn = [0 for _ in range(n)]
done = {i: True for i in range(n)}
for start in range(n):
    if done[start]:
        nex = -1
        j = 0
        tuuka = []
        nax = -float("inf")
        while nex != start:
            if nex == -1:
                nex = P[start]-1
            else:
                nex = P[nex]-1
            tuuka.append(nex)
            score[start] += C[nex]
            nax = max(nax, score[start])
            j += 1
        loopn[start] = j
        for i in tuuka:
            done[i] = False
            loopn[i] = j
            score[i] = score[start]
            nokori = k-(k//loopn[start])*loopn[start]

    if score[start] < 0:
        score[start] = 0
    else:
        score[start] = (k//loopn[start])*score[start]
        nokori = k-(k//loopn[start])*loopn[start]
        i = 0
        nnnnn = (k//loopn[start])
        nscore = 0
        nax = 0
        while i < nokori:
            nex = P[nex]-1
            nscore += C[nex]
            if nscore < 0:
                break
            nax = max(nax, nscore)
            i += 1
        if nax > 0:
            score[start] += nax


ans = max(score)
if ans <= 0:
    ans = max(C)
print(ans)
"""

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))

ans=-float('inf')
for i in range(n):
  T=1
  index=p[i]-1
  score=c[index]
  while index!=i:
    T+=1
    index=p[index]-1
    score+=c[index]
  #場合分け開始
  if T>k:
    score_list=[c[p[i]-1]]+[0]*(k-1)
    index=p[i]-1
    for s in range(1,k):
      index=p[index]-1
      score_list[s]=score_list[s-1]+c[index]
    score=max(score_list)
  else:
    if score<0:
      score_list=[c[p[i]-1]]+[0]*(n-1)
      index=p[i]-1
      for s in range(1,n):       
        index=p[index]-1
        score_list[s]=score_list[s-1]+c[index]        
      score=max(score_list)
    else:
      m1=k//T
      m2=k%T
      score_list=[c[p[i]-1]]+[0]*(m2-1)
      index=p[i]-1
      for s in range(1,m2):
        index=p[index]-1
        score_list[s]=score_list[s-1]+c[index]
      score=max(score_list)+score*m1
  ans=max(ans,score)

print(ans)