Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
a,b,c,d=map(int,input().split())
if !abs(a-b)>d&&!abs(b-c)>d:
    print("Yes")
else:
    print("No")

Fixed submission:
a,b,c,d=map(int,input().split())
if abs (a-c)<=d or (abs(a-b)<=d and abs(b-c)<=d):
    print("Yes")
else:
    print("No")

Buggy python submission (verdict: Runtime Error):
a,b,c,d=map(int,input().split())
if abs(a-b)==d&&abs(b-c)==d:
    print("Yes")
else:
    print("No")

Fixed submission:
a,b,c,d=map(int,input().split())
if abs (a-c)<=d or (abs(a-b)<=d and abs(b-c)<=d):
    print("Yes")
else:
    print("No")

Buggy python submission (verdict: Runtime Error):
a=int(input())
b=int(input())
c=int(input())
d=int(input())
if a+b<=d and b+c<=d:
    print("Yes")
else:
    print("No")


Fixed submission:
a,b,c,d=map(int,input().split())
if abs (a-c)<=d or (abs(a-b)<=d and abs(b-c)<=d):
    print("Yes")
else:
    print("No")

Buggy python submission (verdict: Runtime Error):
a=int(input())
b=int(input())
c=int(input())
d=int(input())
if a+b<=d and c+d<=d:
    print("Yes")
else:
    print("No")

Fixed submission:
a,b,c,d=map(int,input().split())
if abs (a-c)<=d or (abs(a-b)<=d and abs(b-c)<=d):
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Runtime Error):
x=int(input())
i,j=2,1
ans=[]
while True:
    j+=1
    if i**j>x:
        i+=1
        j=2
        if i**j>x:
            break
    ans.append(i**j)
print(max(ans))

Fixed submission:
x=int(input())
i,j=2,1
ans=[]
if x==1:
    print(1)
    quit()
while True:
    j+=1
    if i**j>x:
        i+=1
        j=2
        if i**j>x:
            break
    ans.append(i**j)
print(max(ans))

Buggy python submission (verdict: Runtime Error):
x = int(input())
 
ans=0
for i in range(1,x):
    for j in range(2,x):
        if b**p<=x:
            ans=b**p
        else:
            break
print(ans)

Fixed submission:
x=int(input())
i,j=2,1
ans=[]
if x==1:
    print(1)
    quit()
while True:
    j+=1
    if i**j>x:
        i+=1
        j=2
        if i**j>x:
            break
    ans.append(i**j)
print(max(ans))

Buggy python submission (verdict: Time Limit Exceeded):
x=int(input())
i=0
a=[]
while x>=i**2:
    i+=1
    a.append((i-1)**2)
    j=0
    while x>=i**j:
        j+=1
        a.append((i-1)**j)
print(max(a))

Fixed submission:
x=int(input())
i,j=2,1
ans=[]
if x==1:
    print(1)
    quit()
while True:
    j+=1
    if i**j>x:
        i+=1
        j=2
        if i**j>x:
            break
    ans.append(i**j)
print(max(ans))

Buggy python submission (verdict: Runtime Error):
x=int(input())
i=0
a=[]
while x>=i**2:
    i+=1
    a.append((i-1)**2)
    while x>=i**j:
        j+=1
        a.append((i-1)**j)
print(max(a))

Fixed submission:
x=int(input())
i,j=2,1
ans=[]
if x==1:
    print(1)
    quit()
while True:
    j+=1
    if i**j>x:
        i+=1
        j=2
        if i**j>x:
            break
    ans.append(i**j)
print(max(ans))
Buggy python submission (verdict: Runtime Error):
h,w=map(int,input().split())
S=[]
for i in range(h):
    s=list(map(int,input.split())
    S.append(s)
for i in range(h):
    for j in range(w):
        if S[i+1][j]==S[i][j+1]==S[i-1][j]==S[i][j-1]==".":
            print("No")
            quit()
print("Yes")

Fixed submission:
h,w=map(int,input().split())
S=[]
for i in range(h):
    s=input()
    S.append(s)
for i in range(1,h-1):
    for j in range(1,w-1):
        if S[i+1][j]==S[i][j+1]==S[i-1][j]==S[i][j-1]=="." and S[i][j]=="#":
            print("No")
            quit()
print("Yes")

Buggy python submission (verdict: Runtime Error):
h,w=map(int,input().split())
S=[]
for i in range(h):
    s=list(map(int,input.split()))
    S.append(s)
for i in range(h):
    for j in range(w):
        if S[i+1][j]==S[i][j+1]==S[i-1][j]==S[i][j-1]==".":
            print("No")
            quit()
print("Yes")

Fixed submission:
h,w=map(int,input().split())
S=[]
for i in range(h):
    s=input()
    S.append(s)
for i in range(1,h-1):
    for j in range(1,w-1):
        if S[i+1][j]==S[i][j+1]==S[i-1][j]==S[i][j-1]=="." and S[i][j]=="#":
            print("No")
            quit()
print("Yes")
Buggy python submission (verdict: Runtime Error):
n=int(input())
li=list(map(int,input().split()))
print(max(li)-min(li))

Fixed submission:
n,x=map(int,input().split())
li=[]
for i in range(n):
    m=int(input())
    li.append(m)
x=x-sum(li)
print(n+x//min(li))
Buggy python submission (verdict: Runtime Error):
x,y=map(int,input().split())
if x%y==0:
    print(-1)
else:
    print(a*(b-1))


Fixed submission:
x,y=map(int,input().split())
if x%y==0:
    print(-1)
else:
    print(x*(y-1))

Buggy python submission (verdict: Runtime Error):
a,b=map(int,input().split())
if x%y==0:
    print(-1)
else:
    print(a*(b-1))

Fixed submission:
x,y=map(int,input().split())
if x%y==0:
    print(-1)
else:
    print(x*(y-1))
Buggy python submission (verdict: Time Limit Exceeded):

n,y=map(int,input().split())
i,j,f=0,0,0
for i in range(y//10000+1):
    a=y-10000*i
    for j in range(a//5000+1):
        p=a-5000*j
        for f in range(p//1000+1):
            o=p-1000*f
            if p>(n-(i+j))*1000:
                break
            elif o==0 and i+j+f==n:
                print(i,j,f)
                exit()
print(-1,-1,-1)

Fixed submission:
n,y=map(int,input().split())
i,j,f=0,0,0
for i in range(y//10000+1):
    a=y-10000*i
    for j in range(a//5000+1):
        p=a-5000*j
        if i+j+p//1000==n:
            print(i,j,p//1000)
            exit()
print(-1,-1,-1)

Buggy python submission (verdict: Time Limit Exceeded):
n,y=map(int,input().split())
i,j,f=0,0,0
for i in range(y//10000+1):
    a=y-10000*i
    for j in range(a//5000+1):
        p=a-5000*j
        for f in range(p//1000+1):
            o=p-1000*f
            if o>(n-(i+j))*1000:
                break
            elif o==0 and i+j+f==n:
                print(i,j,f)
                exit()
print(-1,-1,-1)

Fixed submission:
n,y=map(int,input().split())
i,j,f=0,0,0
for i in range(y//10000+1):
    a=y-10000*i
    for j in range(a//5000+1):
        p=a-5000*j
        if i+j+p//1000==n:
            print(i,j,p//1000)
            exit()
print(-1,-1,-1)

Buggy python submission (verdict: Time Limit Exceeded):
n,y=map(int,input().split())
for i in range(y//10000+1):
    a=y-10000*i
    for j in range(a//5000+1):
        p=a-5000*j
        for f in range(p//1000+1):
            o=p-1000*f
            if o==0 and i+j+f==n:
                print(i,j,f)
                exit()
print(-1,-1,-1)

Fixed submission:
n,y=map(int,input().split())
i,j,f=0,0,0
for i in range(y//10000+1):
    a=y-10000*i
    for j in range(a//5000+1):
        p=a-5000*j
        if i+j+p//1000==n:
            print(i,j,p//1000)
            exit()
print(-1,-1,-1)
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
s=[]
cnt=0
for i in range(n):
    while a[i]%2==0:
        cnt+=1
    s.append(cnt)
print(min(s))

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
s=[]
i=0
cnt=0
for i in range(n):
    while a[i]%2==0:
        cnt+=1
        a[i]=a[i]//2
    else:
        s.append(cnt)
        i+=1
        cnt=0
print(min(s))
Buggy python submission (verdict: Runtime Error):
n=int(input())
a=0
for i in range(len(str(n))):
    a+=n[i]
if n%a==0:
    print("Yes")
else:
    print("No")

Fixed submission:
n=input()
a=0
for i in range(len(n)):
    a+=int(n[i])
if int(n)%a==0:
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Runtime Error):
a,b,c,d=map(int,input().split())
if a+b+c+d==7:
    print(a,"+",b,"+",c,"+",d,"=",7,sep="")
elif a+b+c-d==7:
    print(a,"+",b,"+",c,"-",d,"=",7,sep="")
elif a+b-c+d==7:
    print(a,"+",b,"-",c,"+",d,"=",7,sep="")
elif a+b-c-d==7:
    print(a,"+",b,"-",c,"-",d,"=",7,sep="")
elif a-b+c+d==7:
    print(a,"-",b,"+",c,"+",d,"=",7,sep="")
elif a-b+c-d==7:
    print(a,"-",b,"+",c,"-",d,"=",7,sep="")
elif a-b-c+d==7:
    print(a,"-",b,"-",c,"+",d,"=",7,sep="")
elif a-b-c-d==7:
    print(a,"-",b,"-",c,"-",d,"=",7,sep="")

Fixed submission:
s=input()
a=int(s[0])
b=int(s[1])
c=int(s[2])
d=int(s[3])
if a+b+c+d==7:
    print(a,"+",b,"+",c,"+",d,"=",7,sep="")
elif a+b+c-d==7:
    print(a,"+",b,"+",c,"-",d,"=",7,sep="")
elif a+b-c+d==7:
    print(a,"+",b,"-",c,"+",d,"=",7,sep="")
elif a+b-c-d==7:
    print(a,"+",b,"-",c,"-",d,"=",7,sep="")
elif a-b+c+d==7:
    print(a,"-",b,"+",c,"+",d,"=",7,sep="")
elif a-b+c-d==7:
    print(a,"-",b,"+",c,"-",d,"=",7,sep="")
elif a-b-c+d==7:
    print(a,"-",b,"-",c,"+",d,"=",7,sep="")
else:
    print(a,"-",b,"-",c,"-",d,"=",7,sep="")
Buggy python submission (verdict: Runtime Error):
s=input()
if len(s)<4:
    a=“a”
else:
    a=“s[0]”+ “s[1]”+ “s[2]”+ “s[3]”
if a==“YAKI”:
    print(“Yes”)
else:
    print(“No”)

Fixed submission:
s=input()
if len(s)<4:
    a=1
else:
    a=s[0]+ s[1]+ s[2]+ s[3]
if a=='YAKI':
    print('Yes')
else:
    print('No')

Buggy python submission (verdict: Runtime Error):
s=list(input())
a=s[0]+ s[1]+ s[2]+ s[3]
if a=='YAKI':
    print('Yes')
else:
    print('No')

Fixed submission:
s=input()
if len(s)<4:
    a=1
else:
    a=s[0]+ s[1]+ s[2]+ s[3]
if a=='YAKI':
    print('Yes')
else:
    print('No')

Buggy python submission (verdict: Runtime Error):
s=input()
if s[0]==“Y” and s[1]==“A” and s[2]==“K” and s[3]==“I”:
    print(“Yes”)
else:
    print(“No”)

Fixed submission:
s=input()
if len(s)<4:
    a=1
else:
    a=s[0]+ s[1]+ s[2]+ s[3]
if a=='YAKI':
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
list=[]
ans=0
for i in range(n):
    a=int(input())
    list.append(a)
for i in range(n):
    if list.count(list[i])%2!=0:
        ans+=1
print(ans)

Fixed submission:
n=int(input())
list=[]
ans=0
sa=1
sas=0
for i in range(n):
    a=int(input())
    list.append(a)
list=sorted(list)
list.append(10000000000)
for i in range(n+1):
    if i!=0:
        if list[i]==list[i-1]:
            sa+=1
        else:
            sas=sa
            sa=1
            if sas%2!=0:
                ans+=1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
ans=[]
for i in range(n):
    if i%2!=n%2:
        ans.insert(0,a[i])
    else:
        ans.append(a[i])
for i in ans:
    print(i,end=" ")

Fixed submission:
from collections import deque
n=int(input())
a=list(map(int,input().split()))
ans=deque()
for i in range(n):
    if i%2!=n%2:
        ans.appendleft(a[i])
    else:
        ans.append(a[i])
for i in ans:
    print(i,end=" ")

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
ans=[]
for i in range(n):
    if i%2==1:
        ans.append(a[i])
    else:
        ans.insert(0,a[i])
if n%2==0:
    ans.reverse()
for i in ans:
    print(i,end=" ")

Fixed submission:
from collections import deque
n=int(input())
a=list(map(int,input().split()))
ans=deque()
for i in range(n):
    if i%2!=n%2:
        ans.appendleft(a[i])
    else:
        ans.append(a[i])
for i in ans:
    print(i,end=" ")
Buggy python submission (verdict: Runtime Error):
n=int(input())
li=li(map(int,input().split()))
print(max(li)-min(li))

Fixed submission:
n=int(input())
li=list(map(int,input().split()))
print(max(li)-min(li))
Buggy python submission (verdict: Runtime Error):
h,w=map(int,input().split())
a=input()
b=input()
print("#"*(h+w))
print("#"+a+"#")
print("#"+b+"#")
print("#"*(h+w))

Fixed submission:
h,w=map(int,input().split())
print("#"*(w+2))
for i in range(h):
    a=input()
    print("#"+a+"#")
print("#"*(w+2))
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
k=1
for i in range(1,n+1):
    k=k*i
print(k%1000000007)

Fixed submission:
n=int(input())
k=1
for i in range(1,n+1):
    k=k*i%1000000007
print(k)
Buggy python submission (verdict: Runtime Error):
n=int(input())
s=input()
x=0
a=[]
for i in range(n):
    if s[i]==I:
        x+=1
    else:
        x-=1
    a.append(x)
print(max(a))

Fixed submission:
n=int(input())
s=input()
x=0
a=[0]
for i in range(n):
    if s[i]=="I":
        x+=1
    else:
        x-=1
    a.append(x)
print(max(a))
Buggy python submission (verdict: Runtime Error):
w=input()
for i in range(w):
    if w.count(w[i])%2==0:
        ans="Yes"
    else:
        ans="No"
        break
print(ans)

Fixed submission:
w=input()
for i in range(len(w)):
    if w.count(w[i])%2==0:
        ans="Yes"
    else:
        ans="No"
        break
print(ans)
Buggy python submission (verdict: Runtime Error):
x=input()
if x[-1]="s":
  print(x"es")
else:
  print(x"s")

Fixed submission:
x=input()
if x[-1]=="s":
  print(f"{x}es")
else:
  print(f"{x}s")
Buggy python submission (verdict: Runtime Error):
N=input()
n=int(N)
for s in range(n):
  ds1,ds2=input().split()
if ds1==ds2 and d(s+1)1==d(s+1)2 and d(s+2)1==d(s+2)2:
  print("Yes")
else:
  print("No")

Fixed submission:
N=input()
n=int(N)
x=0
for s in range(n):
  ds1,ds2=input().split()
  if ds1==ds2:
    x+=1
  else:
    x=0
  if x==3:
    print("Yes")
    break
else:
  print("No")
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
x=0
for n in range(1,N):
  a=N-n
  for s in range(1,a+1):
    if a % s == 0:
      x+=1
    else:
      x+=0
print(x)

Fixed submission:
N=int(input())
x=0
for n in range(1,N):
  x+=(N-1)//n
print(x)
Buggy python submission (verdict: Runtime Error):
x = input()
if x ==0:
  print(1)
else x == 1:
  print(0)

Fixed submission:
x = input()
y=int(x)
if y==0:
  print("1")
elif y==1:
  print("0")
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
x=1
z=10**9+7
for n in range(1,N+1):
  x*=n
print(x%z)

Fixed submission:
N=int(input())
x=1
z=10**9+7
for n in range(1,N+1):
  x*=n
  x%=z
print(x)
Buggy python submission (verdict: Time Limit Exceeded):
#ABC179-c

import sys,math,collections,itertools
input = sys.stdin.readline

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""

def factorize(n):
    fct = []  # prime factor
    b, e = 2, 0  # base, exponent
    while b * b <= n:
        while n % b == 0:
            n = n // b
            e = e + 1
        if e > 0:
            fct.append((b, e))
        b, e = b + 1, 0
    if n > 1:
        fct.append((n, 1))
    return fct

N = int(input())
cnt = 0
for c in range(1,N-1):
    arr = factorize(N-c)
    tmp = 1
    for ar in arr:
        tmp *= (ar[1]+1)
    cnt += tmp
cnt +=1
print(cnt)


Fixed submission:
#ABC179-c

import sys,math,collections,itertools
input = sys.stdin.readline

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""

def fact_fast(n):
    arr = []
    temp = n
    i=2
    if temp%i==0:
        cnt=0
        while temp%i==0:
            cnt+=1
            temp //= i
        arr.append([i, cnt])
    i =3
    while i*i<=temp:
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
        i += 2
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr

N = int(input())
cnt = 0
for c in range(1,N-1):
    arr = fact_fast(N-c)
    tmp = 1
    for ar in arr:
        tmp *= (ar[1]+1)
    cnt += tmp
cnt +=1
print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
#abc176-d
import sys,math,collections,itertools
input = sys.stdin.readline

H,W=list(map(int,input().split()))
Ch,Cw=list(map(int,input().split()))
Dh,Dw=list(map(int,input().split()))
s=[ input().rstrip() for _ in range(H)]

#DPで遷移が2種類　初期値をinf として、現在値より小さければ遷移可能とする
dp = [ [float('inf') ]*W  for _ in range(H)]

q = collections.deque([])
q.append((Ch-1,Cw-1))
dp[Ch-1][Cw-1]=0
while q:
    nh,nw = q.popleft()
    #徒歩での遷移
    if nh>0 and s[nh-1][nw] =='.' and dp[nh-1][nw]>dp[nh][nw]:
        q.append((nh-1,nw))
        dp[nh-1][nw] = dp[nh][nw]
    if nh<H-1 and s[nh+1][nw] =='.' and dp[nh+1][nw]>dp[nh][nw]:
        q.append((nh+1,nw))
        dp[nh+1][nw] = dp[nh][nw]
    if nw>0 and s[nh][nw-1] =='.' and dp[nh][nw-1]>dp[nh][nw]:
        q.append((nh,nw-1))
        dp[nh][nw-1] = dp[nh][nw]
    if nw<W-1 and s[nh][nw+1] =='.' and dp[nh][nw+1]>dp[nh][nw]:
        q.append((nh,nw+1))
        dp[nh][nw+1] = dp[nh][nw]
    #ワープで遷移
    for ih in range(-2,3,1):
        for iw in range(-2,3,1):
            if (0<= nh+ih<=H-1) and (0<=nw+iw<=W-1):
                if s[nh+ih][nw+iw] =='.' and dp[nh+ih][nw+iw]>dp[nh][nw]:
                    q.append((nh+ih,nw+iw))
                    dp[nh+ih][nw+iw] = dp[nh][nw]+1


ans = dp[Dh-1][Dw-1]
print(-1 if ans==float('inf') else ans)    
    




Fixed submission:
#abc176-d
import sys,math,collections,itertools
input = sys.stdin.readline
 
H,W=list(map(int,input().split()))
Ch,Cw=list(map(int,input().split()))
Dh,Dw=list(map(int,input().split()))
s=[ input().rstrip() for _ in range(H)]
 
#DPで遷移が2種類　初期値をinf として、現在値より小さければ遷移可能とする
dp = [ [float('inf') ]*W  for _ in range(H)]
 
q = collections.deque([])
q.append((Ch-1,Cw-1))
dp[Ch-1][Cw-1]=0
while q:
    visit = []
    while q:
        nh,nw = q.popleft()
        visit.append((nh,nw))
        #徒歩での遷移
        if nh>0 and s[nh-1][nw] =='.' and dp[nh-1][nw]>dp[nh][nw]:
            q.append((nh-1,nw))
            dp[nh-1][nw] = dp[nh][nw]
        if nh<H-1 and s[nh+1][nw] =='.' and dp[nh+1][nw]>dp[nh][nw]:
            q.append((nh+1,nw))
            dp[nh+1][nw] = dp[nh][nw]
        if nw>0 and s[nh][nw-1] =='.' and dp[nh][nw-1]>dp[nh][nw]:
            q.append((nh,nw-1))
            dp[nh][nw-1] = dp[nh][nw]
        if nw<W-1 and s[nh][nw+1] =='.' and dp[nh][nw+1]>dp[nh][nw]:
            q.append((nh,nw+1))
            dp[nh][nw+1] = dp[nh][nw]
    while visit:
    #ワープで遷移
        nh,nw = visit.pop()
        for ih in range(-2,3,1):
            for iw in range(-2,3,1):
                if (0<= nh+ih<=H-1) and (0<=nw+iw<=W-1):
                    if s[nh+ih][nw+iw] =='.' and dp[nh+ih][nw+iw]>dp[nh][nw]:
                        q.append((nh+ih,nw+iw))
                        dp[nh+ih][nw+iw] = dp[nh][nw]+1
     
 
ans = dp[Dh-1][Dw-1]
print(-1 if ans==float('inf') else ans)
Buggy python submission (verdict: Time Limit Exceeded):
#--d--#
import sys,math,collections,itertools
input = sys.stdin.readline

def solve(i):#-スタートする番号-#
    visit = set([i])
    tmp = [] #訪れた場所から飛ぶ時の得点
    #tmp_sum = [] #得点の累積和
    while True:
        tmp.append(C[i-1])
        next_point = P[i-1]
        if next_point not in visit:
            visit |=set([next_point])
            i = next_point
        else:
            break
    roopLength = len(tmp)
    roop_sum = sum(tmp)
    cum_sum = list(itertools.accumulate(tmp))
    #print(roopLength,tmp,list(cum_sum))
    #print(visit)
    #-もしループで負になるなら、roopLength or Kの小さいほうの中でcum_sumの大きいもの
    if roop_sum <=0:
        ans = -float('inf')
        for i in range(min(K,roopLength)):
            ans = max(ans,cum_sum[i])
    #ループ長さｘ２がKよりおおきいなら、Kまで足し上げる
    elif roopLength * 2 >=K:
        point = 0
        ans = -float('inf')
        for i in range(K):
            point +=  tmp[i%roopLength]
            ans = max(ans,point)
    #ループ長さｘ２がKみ満たないなら、２週分残したとこまでループさせて、足し上げる
    elif roopLength *2 < K:
        point = roop_sum*(K//roopLength-1)
        ans = -float('inf')
        for i in range(roopLength+K%roopLength):
            point += tmp[i%roopLength]
            ans = max(ans,point)
    return ans


N,K=list(map(int,input().split()))
P=list(map(int,input().split()))
C=list(map(int,input().split()))
ans = -float('inf')
for i in range(N):
    point = solve(i+1)
    ans = max(point,ans)
print(ans)
    



Fixed submission:
#--d--#
import sys,math,collections,itertools
input = sys.stdin.readline

def solve(i):#-スタートする番号-#
    visit = set([i])
    tmp = [] #訪れた場所から飛ぶ時の得点
    #tmp_sum = [] #得点の累積和
    while True:
        tmp.append(C[i-1])
        next_point = P[i-1]
        if next_point not in visit:
            visit |=set([next_point])
            i = next_point
        else:
            break
    roopLength = len(tmp)
    roop_sum = sum(tmp)
    cum_sum = list(itertools.accumulate(tmp))
    #print(roopLength,tmp,list(cum_sum))
    #print(visit)
    #-もしループで負になるなら、roopLength or Kの小さいほうの中でcum_sumの大きいもの
    if roop_sum <=0:
        ans = -float('inf')
        for i in range(min(K,roopLength)):
            ans = max(ans,cum_sum[i])
    #ループ長さｘ２がKよりおおきいなら、Kまで足し上げる
    elif roopLength * 2 >=K:
        point = 0
        ans = -float('inf')
        for i in range(K):
            point +=  tmp[i%roopLength]
            ans = max(ans,point)
    #ループ長さｘ２がKみ満たないなら、２週分残したとこまでループさせて、足し上げる
    elif roopLength *2 < K:
        point = roop_sum*(K//roopLength-1)
        ans = -float('inf')
        for i in range(roopLength+K%roopLength):
            point += tmp[i%roopLength]
            ans = max(ans,point)
    return ans


N,K=list(map(int,input().split()))
P=list(map(int,input().split()))
C=list(map(int,input().split()))
ans = -float('inf')
for i in range(N):
    point = solve(i+1)
    ans = max(point,ans)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

R,C,K=list(map(int,input().split()))
itemMap = [[0]*(C+1) for _ in range(R+1)]
dp =[[[0]*(4) for _ in range(C+1)] for _ in range(R+1)]

for _ in range(K):
    r,c,v=list(map(int,input().split()))
    itemMap[r-1][c-1]=v

for ir in range(R):
    for ic in range(C):
        for k in range(2,-1,-1):
            #-拾う拾わない遷移-#
            dp[ir][ic][k+1] = max(dp[ir][ic][k+1],dp[ir][ic][k]+itemMap[ir][ic])
        #-位置遷移-#
        for k in range(4):
            dp[ir+1][ic][0] = max(dp[ir+1][ic][0],dp[ir][ic][k])
            dp[ir][ic+1][k] = max(dp [ir][ic+1][k],dp[ir][ic][k])

ans = 0
for k in range(4):
    ans = max(ans,dp[R-1][C-1][k])
print(ans)
                            
                        


Fixed submission:
import sys
input = sys.stdin.readline

R,C,K=list(map(int,input().split()))
itemMap = [[0]*(C+1) for _ in range(R+1)]
dp =[[[0]*(C+1) for _ in range(R+1)] for _ in range(4)]

for _ in range(K):
    r,c,v=list(map(int,input().split()))
    itemMap[r-1][c-1]=v

for ir in range(R):
    for ic in range(C):
        for k in range(2,-1,-1):
            #-拾う拾わない遷移-#
            dp[k+1][ir][ic] = max(dp[k+1][ir][ic],dp[k][ir][ic]+itemMap[ir][ic])
        #-位置遷移-#
        for k in range(4):
            dp[0][ir+1][ic] = max(dp[0][ir+1][ic],dp[k][ir][ic])
            dp[k][ir][ic+1] = max(dp[k][ir][ic+1],dp[k][ir][ic])

ans = 0
for k in range(4):
    ans = max(ans,dp[k][R-1][C-1])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys,math,collections,itertools
input = sys.stdin.readline

R,C,K=list(map(int,input().split()))
itemMap = [[0]*(C+1) for _ in range(R+1)]
dp =[[[0]*(4) for _ in range(C+1)] for _ in range(R+1)]

for _ in range(K):
    r,c,v=list(map(int,input().split()))
    itemMap[r-1][c-1]=v

for ir in range(R):
    for ic in range(C):
        for k in range(2,-1,-1):
            #-拾う拾わない遷移-#
            dp[ir][ic][k+1] = max(dp[ir][ic][k+1],dp[ir][ic][k]+itemMap[ir][ic])
        #-位置遷移-#
        for k in range(4):
            dp[ir+1][ic][0] = max(dp[ir+1][ic][0],dp[ir][ic][k])
            dp[ir][ic+1][k] = max(dp [ir][ic+1][k],dp[ir][ic][k])

ans = 0
for k in range(4):
    ans = max(ans,dp[R-1][C-1][k])
print(ans)
                            
                        


Fixed submission:
import sys
input = sys.stdin.readline

R,C,K=list(map(int,input().split()))
itemMap = [[0]*(C+1) for _ in range(R+1)]
dp =[[[0]*(C+1) for _ in range(R+1)] for _ in range(4)]

for _ in range(K):
    r,c,v=list(map(int,input().split()))
    itemMap[r-1][c-1]=v

for ir in range(R):
    for ic in range(C):
        for k in range(2,-1,-1):
            #-拾う拾わない遷移-#
            dp[k+1][ir][ic] = max(dp[k+1][ir][ic],dp[k][ir][ic]+itemMap[ir][ic])
        #-位置遷移-#
        for k in range(4):
            dp[0][ir+1][ic] = max(dp[0][ir+1][ic],dp[k][ir][ic])
            dp[k][ir][ic+1] = max(dp[k][ir][ic+1],dp[k][ir][ic])

ans = 0
for k in range(4):
    ans = max(ans,dp[k][R-1][C-1])
print(ans)