qyliang/CodeNanoFix huggingface dataset:
Buggy code:
N=int(input())
count=0
L=list(map(int,input().split()))
for i in range(N):
  for j in range(i,N):
    for k in range(j,N):
      if L[i]<L[j]+L[k] and L[i]+L[j]>L[k] and L[j]<L[i]+L[k]:
        count+=1
print(count)

Fixed code:
N=int(input())
count=0
L=list(map(int,input().split()))
for i in range(N):
    for j in range(i+1,N):
        for k in range(j+1,N):
            if L[i]<L[j]+L[k] and L[j]<L[k]+L[i] and L[k]<L[i]+L[j]:
                if L[i]!=L[k] and L[i]!=L[j] and L[k]!=L[j]:
                    count+=1
print(count)
Buggy code:
n=input()
l=map(int,input().split())
li=sorted(l)
for i in range(n):
    for j in range(i,n+1):
        for k in range(j,n+1):
            if li[i]!=li[j]!=li[k] and li[i]+li[j]>li[k]:
                ans+=1
print(ans)

Fixed code:
n=int(input())
ans=0
l=map(int,input().split())
li=sorted(l)
for i in range(n):
    for j in range(i+1,n):
        for k in range(j+1,n):
            if li[i]!=li[j]!=li[k] and li[i]+li[j]>li[k]:
                ans+=1
print(ans)
Buggy code:
N = int(input())
A = list(map(int,input().split()))
cnt = 0
A.sort()
for i in range(0,N):
  for j in range(i+1,N):
    for k in range(j+1,N):
      if (A[i]+A[j])>A[k] and (A[j]+[k])>A[i] and (A[i]+A[k])>A[j] and 
      A[i]!=A[k] and A[j]!=A[k] and A[i]!=A[j]:
        cnt+=1
        
print(cnt)

Fixed code:
N = int(input())
A = list(map(int,input().split()))
A.sort()
cnt = 0
for i in range(0,N):
  for j in range(i+1,N):
    for k in range(j+1,N):
      if (A[i]!=A[j]!=A[k]):
        if (A[i]+A[j])>A[k]:
          cnt+=1
        
print(cnt)
Buggy code:
n = int(input())
l = list(map(int, input().split()))

l.sort(reverse=True)
ans = 0
for k in range(0, n-2):
  for j in range(k+1, n-1):
    if l[k]/2 > l[j]:
      break
    for i in range(j+1, n):
      if l[k] > l[j] + l[i]:
        continue
      else:
        ans += 1
print(ans)

Fixed code:
n = int(input())
l = list(map(int, input().split()))

l.sort(reverse=True)
ans = 0
if n < 3:
  ans = 0
else:
  for k in range(0, n-2):
    for j in range(k+1, n-1):
      if l[k]/2 > l[j]:
        break
      if l[k] == l[j]:
        continue
      for i in range(j+1, n):
        if l[k] >= l[j] + l[i] or l[j] == l[i]:
          continue
        else:
          ans += 1
print(ans)
Buggy code:
n = int(input())
    l = list(map(int, input().split()))
    ans = 0

    for i in range(n - 2):
        for j in range(i + 1, n - 1):
            for k in range(j + 1, n):
                if len(set([l[i], l[j], l[k]])) == 3 and max(l[i], l[j], l[k]) < sum([l[i], l[j], l[k]]) - max(l[i], l[j], l[k]):
                    ans += 1
    print(ans)

Fixed code:
n = int(input())
l = list(map(int, input().split()))
ans = 0
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        for k in range(j + 1, n):
            if len(set([l[i], l[j], l[k]])) == 3 and max(l[i], l[j], l[k]) < sum([l[i], l[j], l[k]]) - max(l[i], l[j], l[k]):
                ans += 1
print(ans)
Buggy code:
import numpy as np

N = int(input())
ls = [int(s) for s in input().split()]

count = 0

for i in range(0, N-2):
  for j in range(i+1, N-1):
    l1 = ls[i]
    l2 = ls[j]
    if l2 == l1:
      continue
    for k in range(j+1, N):
      l3 = ls[k]
      if l3 in {l1, l2}:
        continue
      max_num = max((l1, l2, l3))
      if 2 * max_num < l1 + l2 + l3:
        print("OK", ls[i], ls[j], ls[k])
        count += 1

print(count)

Fixed code:
import numpy as np

N = int(input())
ls = [int(s) for s in input().split()]

count = 0

for i in range(0, N-2):
  for j in range(i+1, N-1):
    l1 = ls[i]
    l2 = ls[j]
    if l2 == l1:
      continue
    for k in range(j+1, N):
      l3 = ls[k]
      if l3 in {l1, l2}:
        continue
      max_num = max((l1, l2, l3))
      if 2 * max_num < l1 + l2 + l3:
        count += 1

print(count)
Buggy code:
N=int(input())
L=list(map(int, input().split()))
count=0
for i in range(N):
    for j in range(i+1,N):
        for k in range(j+1,N):
            L2=[L[i],L[j],L[k]]
            L2.sort()
            print(L2)
            if L2[0]+L2[1]>L2[2] and L2[0] != L2[1] and L2[1] != L2[2] and L2[2] != L2[0]:
                count+=1
                
print(count)


Fixed code:
N=int(input())
L=list(map(int, input().split()))
count=0
for i in range(N):
    for j in range(i+1,N):
        for k in range(j+1,N):
            L2=[L[i],L[j],L[k]]
            L2.sort()
            if L2[0]+L2[1]>L2[2] and L2[0] != L2[1] and L2[1] != L2[2] and L2[2] != L2[0]:
                count+=1
                
print(count)
Buggy code:
N=int(input())
L=list(map(int,input().split()))
L.sort()
print(L)
c=0
for s in range(N-2):
	for t in range(s+1,N-1):
		two=L[s]+L[t]
		if L[s] != L[t]:
			for u in range(t+1,N):
				if L[u] == L[t]:
					c+=0
				elif two > L[u]:
					c+=1
					#print(s,t,u)
				else:
					break
print(c)

Fixed code:
N=int(input())
L=list(map(int,input().split()))
L.sort()
c=0
for s in range(N-2):
	for t in range(s+1,N-1):
		two=L[s]+L[t]
		if L[s] != L[t]:
			for u in range(t+1,N):
				if L[u] == L[t]:
					c+=0
				elif two > L[u]:
					c+=1
					#print(s,t,u)
				else:
					break
print(c)
Buggy code:
n=int(input())
l=list(map(int,input().split()))
l.sort(reverse=True)
d=dict()
for i in l:
  if i in d:
    d[i]+=1
  else:
    d[i]=1
#print(d)
li=list(d.keys())  
ans=0
for i in range(len(li)-1,0,-1):
  a,b=0,i-1
  while(a<b):
    if li[a]+li[b]>li[i]:
      #print(li[a],li[b],li[i])
      ans+=d[li[a]]*d[li[b]]*d[li[i]]
      ans+=b-1
      b-=1
    else:
      a+=1
print(ans)

Fixed code:
n=int(input())
l=list(map(int,input().split()))
l.sort()
d=dict()
for i in l:
  if i in d:
    d[i]+=1
  else:
    d[i]=1
#print(d)
li=list(d.keys()) 
ans=0
for i in range(len(li)-1,0,-1):
  a,b=0,i-1
  while(a<b):
    if li[a]+li[b]>li[i]:
      #print(li[a],li[b],li[i])
      for kk in range(a,b):
      	ans+=d[li[kk]]*d[li[b]]*d[li[i]]
      b-=1
    else:
      a+=1
print(ans)
Buggy code:
n=int(input())
boulist=list(map(int,input().split()))
boulist.sort(reverse=True)
print(boulist)
count=0
for i in range(n):
  for j in range(i,n):
    for k in range(j,n):
      if boulist[j] != boulist[k] and boulist[i] != boulist[j] and boulist[i]+boulist[j]>boulist[k]:
        count+=1

print(count)




Fixed code:
n=int(input())
boulist=list(map(int,input().split()))
boulist.sort(reverse=True)

count=0
for i in range(n):
  for j in range(0,i):
    for k in range(0,j):
      if boulist[j] != boulist[k] and boulist[i] != boulist[j] and boulist[i]+boulist[j]>boulist[k]:
        count+=1

print(count)
Buggy code:
n,k=map(int,input().split())
P=list(map(int,input().split()))
for i in range(n):
	P[i]-=1
C=list(map(int,input().split()))
ANS=-float('inf')
for i in range(n):
	snp=P[i]
	r=C[snp]
	L=[r]
	c=1
	np=P[snp]
	while True:
		r+=C[np]
		L.append(r)
		if c==k-1:
			ANS=max(max(L),ANS)
			break
		if np==snp:
			y=k%c
			m=k//c
			if y==0:
				ANS=max(max(L),max(L)+m*sum(L),ANS)
			else:
				ANS=max(max(L[:y]),max(L[:y])+m*sum(L),ANS)
			break
		np=P[np]
		c+=1
print(ANS)


Fixed code:
n,k=map(int,input().split())
P=list(map(int,input().split()))
for i in range(n):
	P[i]-=1
C=list(map(int,input().split()))
ANS=-float('inf')
for i in range(n):
    snp=P[i]
    r=C[snp]
    L=[r]
    c=0
    np=P[snp]
    while True:
        if c==k-1:
            ANS=max(max(L),ANS)
            break
        if np==snp:
            y=k%(c+1)
            m=k//(c+1)
            if y==0:
                ANS=max(max(L),max(L)+(m-1)*L[-1],ANS)
                break
            else:
                #print(m,y)
                #print(L)
                ANS=max(max(L),max(max(L[:y]),0)+m*L[-1],ANS)
                break
        r+=C[np]
        L.append(r)
        np=P[np]
        c+=1
print(ANS)
Buggy 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):
    hoge = []
    p = i
    s = 0
    for j in range(K):
        p = P[p]-1
        s += C[p]
        hoge.append(s)
        ans = max(ans,s)
        if p == i:
            s = max(s*((K-1)//len(hoge)) + max(hoge[:(K-1)%len(hoge)+1]),s*((K-1)//len(hoge)-1) + max(hoge) )
            ans = max(ans,s)
            break
print(ans)
        

Fixed 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):
    hoge = []
    p = i
    s = 0
    for j in range(K):
        p = P[p]-1
        s += C[p]
        hoge.append(s)
        ans = max(ans,s)
        if p == i:
            S1 = s*((K-1)//len(hoge))
            S1 += max(hoge[0:(K-1)%len(hoge)+1])
            S2 = ans
            if (K-1) // len(hoge) > 1:
                S2 = s*((K-1)//len(hoge)-1)
                S2 += max(hoge)
            ans = max(ans,S1,S2)
            break
print(ans)
Buggy code:
N, K = map(int,input().split())
P = list(map(int,input().split()))
C = list(map(int,input().split()))
ans = 0
for i in range(N):
    hoge = []
    p = i
    s = 0
    for j in range(K):
        p = P[p]-1
        s += C[p]
        hoge.append(s)
        ans = max(ans,s)
        if p == i:
            s *= K//len(hoge)
            if K%len(hoge) != 0:
                s += max(hoge[0:K%len(hoge)])
            ans = max(ans,s)
            break
print(ans)
        

Fixed 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):
    hoge = []
    p = i
    s = 0
    for j in range(K):
        p = P[p]-1
        s += C[p]
        hoge.append(s)
        ans = max(ans,s)
        if p == i:
            s *= (K-1)//len(hoge)
            s += max(hoge[0:(K-1)%len(hoge)+1])
            ans = max(ans,s)
            break
print(ans)
Buggy code:
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")

n, k = map(int, input().split())
p = [int(x) - 1 for x in input().split()]
c = [int(x) for x in input().split()]

ans = -float('inf')
for i in range(n):
    now = i
    c_sum = [0]
    s = set()
    cnt = 0
    while True:
        s.add(now)
        c_sum.append(c_sum[-1] + c[now])
        cnt += 1
        now = p[now]
        if now in s:
            break
    if k < cnt:
        ans = max(ans, max(c_sum[1: k + 1]))
    else:
        score = c_sum[-1]
        count = k // cnt
        k_ = k - count*cnt
        score *= count
        score += max(c_sum[1: k_ + 1])
        ans = max(ans, score, max(c_sum[1: ]))
print(ans)




    

Fixed code:
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")

n, k = map(int, input().split())
p = [int(x) - 1 for x in input().split()]
c = [int(x) for x in input().split()]

ans = -float('inf')
for i in range(n):
    now = i
    c_sum = [0]
    s = set()
    cnt = 0
    while True:
        s.add(now)
        c_sum.append(c_sum[-1] + c[now])
        cnt += 1
        now = p[now]
        if now in s:
            break
    c_sum = c_sum[1:]
    if k < cnt:
        ans = max(ans, max(c_sum[: k]))
    else:
        score = c_sum[-1]
        count = k // cnt
        k_ = k - count*cnt
        score *= count
        score_2 = score - c_sum[-1]
        score_2 += max(c_sum)
        if k_ > 0:
            score += max(0, max(c_sum[:k_]))
        ans = max(ans, score, max(c_sum), score_2)
print(ans)
Buggy 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):
   i += 1
   start = i
   number = 1
   circle = [c[i-1]]
   i = p[i-1]
   while i != start:
      number += 1
      l = len(circle)
      score = circle[l-1] + c[i-1]
      circle.append(score)
      i = p[i-1]
   for j in range(min(l, K+1):
      if circle[l-1] > 0:
         score = circle[j] + ((K - j)//l)*circle[l-1]
      else:
         score = circle[j]
      ans = max(score, ans)      
print(ans)

Fixed 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 h in range(N):
   i = h+1
   start = i
   circle = [c[i-1]]
   i = p[i-1]
   while i != start:
      score = circle[-1] + c[i-1]
      circle.append(score)
      i = p[i-1]
   l=len(circle)
   for j in range(min(l, K+1)):
      if circle[l-1] > 0:
         score = circle[j] + ((K - j)//l)*circle[l-1]
      else:
         score = circle[j]
      ans = max(score, ans)      
print(ans)
Buggy code:
N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
 
ans = -10 * 10
for s in range(N):
    S = [] 
    i = P[s] - 1
    S.append(C[i])
    while i != s:
        i = P[i] - 1
        S.append(S[-1] + C[i])
 
    if K <= len(S):
        score = max(S[:K])
    elif S[-1] <= 0:
        score = max(S)
    else:
        score1 = S[-1] * (K // len(S) - 1)
        score1 += max(S)
        score2 = S[-1] * (K // len(S))
        r = K % len(S)
        if r != 0:
            score2 += max(0, max(S[:r]))
        score = max(score1, score2)
 
    ans = max(ans, score)
 
print(ans)

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

ans = -float('inf')
for s in range(N):
    S = []

    S.append(C[s])
    i = P[s] - 1

    while i != s:
        S.append(S[-1] + C[i])
        i = P[i] - 1
    if K <= len(S):
        score = max(S[:K])
    elif S[-1] <= 0:
        score = max(S)
    else:
        score1 = S[-1] * (K // len(S) - 1)
        score1 += max(S)
        score2 = S[-1] * (K // len(S))
        r = K % len(S)
        if r != 0:
            score2 += max(0, max(S[:r]))
        score = max(score1, score2)
    ans = max(ans, score)
print(ans)
Buggy code:
n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
ttl=[]
cnt=[]
count=0
for i in range(n):
  nxt=i+1
  grp=[]
  count=0
  pnt=[]
  pnt.append(0)
  for j in range(n):
    nxt=p[nxt-1]
    grp.append(nxt)
    pnt.append(pnt[j]+c[nxt-1])
    count+=1
    if nxt==i+1:
      del pnt[0]
      ttl.append(pnt)
      cnt.append(len(grp))
      break
      
ans=[]
for i in range(n):
  peak=ttl[i].index(max(ttl[i]))
  if k<=cnt[i]:
    ans.append(max(ttl[i][:k]))
  else:
    if ttl[i][-1]<0:
      ans.append(max(ttl[i][:k]))
    else:
      ans.append(max((k//cnt[i])*ttl[i][-1]+max(ttl[i][:k%cnt[i]]),((k-peak)//cnt[i])*ttl[i][-1]+ttl[i][peak]))
print(max(ans))

Fixed code:
n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
ttl=[]
cnt=[]
count=0
for i in range(n):
  nxt=i+1
  grp=[]
  count=0
  pnt=[]
  pnt.append(0)
  for j in range(n):
    nxt=p[nxt-1]
    pnt.append(pnt[j]+c[nxt-1])
    count+=1
    if nxt==i+1:
      del pnt[0]
      ttl.append(pnt)
      cnt.append(count)
      break
ans=[]
for i in range(n):
  peak=ttl[i].index(max(ttl[i]))
  if k<=cnt[i]:
    ans.append(max(ttl[i][:k]))
  else:
    if ttl[i][-1]<0:
      ans.append(ttl[i][peak])
    else:
      if k%cnt[i]==0:
        ans.append(((k-peak-1)//cnt[i])*ttl[i][-1]+ttl[i][peak])
      else:
        ans.append(max((k//cnt[i])*ttl[i][-1]+max(ttl[i][0:k%cnt[i]]),((k-peak-1)//cnt[i])*ttl[i][-1]+ttl[i][peak]))
print(max(ans))
Buggy code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n,k = na()
p = na()
c = na()

s = set(range(n))

ans = 0
loops = []
while s:
    loop = [0]
    a = s.pop()
    while True:
        loop.append(loop[-1]+c[p[a]-1])
        if p[a]-1 not in s:
            break
        a = p[a]-1
        s.discard(a)
    loops.append(loop[1:])
    
ans = 0
for tmp in loops:
    kk = min(k%len(tmp), k)
    loop = tmp+[tmp[-1]+u for u in tmp]
    x = 0
    for i in range(1,kk+1):
        for j in range(len(tmp)):
            x = max(x,loop[j+i]-loop[j])
    if k > len(tmp) and tmp[-1] > 0:
        x += tmp[-1]*(k//len(tmp))
    ans = max(ans,x)

print(ans)

Fixed code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n,k = na()
p = na()
c = na()

s = set(range(n))

ans = 0
loops = []
while s:
    loop = [0]
    a = s.pop()
    while True:
        loop.append(loop[-1]+c[p[a]-1])
        if p[a]-1 not in s:
            break
        a = p[a]-1
        s.discard(a)
    loops.append(loop[1:])

ans = -1000000000000000
for tmp in loops:
    kk = k
    if k > len(tmp): kk = k%len(tmp)+len(tmp)
    loop = [0]+tmp+[tmp[-1]+u for u in tmp]+[tmp[-1]*2+u for u in tmp]
    x = -1000000000000
    for i in range(1,kk+1):
        for j in range(len(tmp)+1):
            x = max(x,loop[j+i]-loop[j])
    if k > len(tmp) and tmp[-1] > 0:
        x += tmp[-1]*(k//len(tmp)-1)
    ans = max(ans,x)

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

step_score = [0] * N
max_score = -1 * float('inf')

for start_pos in range(N):
    pos = start_pos
    loop_num = 0
    loop_sum = 0
    step = 0
    while (step < K and step < N):
        pos = P[pos]
        loop_sum += C[pos]
        step_score[step] = loop_sum
        step += 1
        if pos == start_pos:
            break
    loop_num = step
    step_max_score = max(step_score)
    if K // loop_num > 0:
        if K % loop_num != 0:
            step_max_score = max(
                step_max_score,
                max(step_score[:(K % loop_num)]) + loop_sum * (K // loop_num)
            )
        else:
            step_max_score = max(
                step_max_score,
                max(step_score) + loop_sum * (K // loop_num)
            )
    if K // loop_num > 1:
        step_max_score = max(
            step_max_score,
            max(step_score) + loop_sum * (K // loop_num - 1)
        )
    max_score = max(step_max_score, max_score)
    # print(start_pos, step_max_score, max(step_score), loop_sum, loop_num)

print(max_score)

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

max_score = -1 * float('inf')

for start_pos in range(N):
    step_score = []
    pos = start_pos
    loop_num = 0
    loop_sum = 0
    step = 0
    while (step < K):
        pos = P[pos]
        loop_sum += C[pos]
        step_score.append(loop_sum)
        step += 1
        if pos == start_pos:
            break
    loop_num = step
    step_max_score = max(step_score)
    if loop_num < K:
        if K // loop_num > 0:
            if K % loop_num != 0:
                step_max_score = max(
                    step_max_score,
                    max(step_score[:(K % loop_num)]) + loop_sum * (K // loop_num)
                )
            else:
                step_max_score = max(
                    step_max_score,
                    loop_sum * (K // loop_num)
                )
        if K // loop_num > 1:
            step_max_score = max(
                step_max_score,
                max(step_score) + loop_sum * (K // loop_num - 1)
            )
    max_score = max(step_max_score, max_score)
    #print(start_pos, step_max_score, max(step_score), loop_sum, loop_num)

print(max_score)
Buggy code:
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))

n,k=nii()
p=lnii()
c=lnii()

ans=-10**18
for i in range(n):
  ini_x=i
  x=i
  t_max=-10**18
  t_ans=[0]
  while True:
    next_x=p[x]-1
    point=c[next_x]
    if t_ans:
      t_ans.append(t_ans[-1]+point)
    t_max=max(t_max,t_ans[-1])
    if next_x==ini_x:
      break
    else:
      x=next_x

  if t_ans[-1]<0:
    ans=max(ans,t_max)
  else:
    num=k//len(t_ans)
    num-=1
    c_ans=t_ans[-1]*num
    c_max=c_ans
    zan=k-(num*len(t_ans))
    s_list=t_ans*2
    for j in range(zan):
      c_ans+=s_list[j]
      c_max=max(c_max,c_ans)
    ans=max(ans,c_max)

print(ans)

Fixed code:
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))

n,k=nii()
p=lnii()
c=lnii()

ans=-10**18
for i in range(n):
  ini_x=i
  x=i
  point=[]

  t_ans=0
  cnt=0
  t_max=-10**18
  while True:
    x=p[x]-1
    t_point=c[x]
    point.append(t_point)
    t_ans+=t_point
    t_max=max(t_max,t_ans)
    cnt+=1
    if x==ini_x:
      break

  if t_ans<0:
    ans=max(ans,t_max)
  else:
    num=k//cnt
    num-=1
    c_ans=t_ans*num
    c_max=c_ans
    zan=k-(num*cnt)
    for j in range(zan):
      x=p[x]-1
      c_ans+=c[x]
      c_max=max(c_max,c_ans)
    ans=max(ans,c_max)

print(ans)
Buggy code:
def main():
    import sys
    N,K=list(map(int,sys.stdin.readline().split()))
    P=list(map(int,sys.stdin.readline().split()))
    C=list(map(int,sys.stdin.readline().split()))

    output=-10**20

    lst=[[0]*(N+1) for _ in range(N)]
    for i in range(N):
        st=set()
        now=i
        count=0
        score=0
        if K==1:
            output=max(output,C[P[i]])
        else:
            while True:
                count+=1
                now=P[now]-1

                if now in st:
                    count-=1
                    break
        
                lst[i][count]=lst[i][count-1]+C[now]
                st.add(now)
                
                if count==K:
                    break
            
            if lst[i][count]>0 and K>=count:
                if K%count:
                    s=lst[i][count]*(K//count)+max(lst[i][1:K%count+1])
                else:
                    s=lst[i][count]*(K//count)
            else:
                s=max(lst[i][1:count+1])
            output=max(output,s)

    print(lst)
    print(output)

main()

Fixed code:
def main():
    import sys
    N,K=list(map(int,sys.stdin.readline().split()))
    P=list(map(int,sys.stdin.readline().split()))
    C=list(map(int,sys.stdin.readline().split()))

    output=-10**20

    lst=[[0]*(N+1) for _ in range(N)]
    for i in range(N):
        st=set()
        now=i
        count=0
        score=0
        if K==1:
            output=max(output,C[P[i]-1])
            continue

        while True:
            count+=1
            now=P[now]-1

            if now in st:
                count-=1
                break
    
            lst[i][count]=lst[i][count-1]+C[now]
            st.add(now)
            
            if count==K:
                break


        if lst[i][count]>0:
            m=max(lst[i][1:count+1])
            
            if K%count:
                p=max(lst[i][1:K%count+1])
                if p>0:
                    s=lst[i][count]*(K//count)+p
                else:
                    s=lst[i][count]*(K//count-1)+m
            else:
                s=lst[i][count]*(K//count-1)+m
        else:
            s=max(lst[i][1:count+1])
        
        output=max(output,s)

    print(output)

main()