Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
P=[None]*N
for i in range(N):
    P[i]=int(input())

#最大長の「一つずつ増加する部分列」を求めればよい

ZOUKA=[0]*(N+1)

for i in range(1,N+1):
    if ZOUKA[i]==-1:
        continue
    
    r=i
    for j in range(N):
        if P[j]==r:
            r+=1
            ZOUKA[i]+=1
            break

    for k in range(j+1,N):
        if P[k]==r:
            ZOUKA[r]=-1
            r+=1
            ZOUKA[i]+=1

Ans=N-max(ZOUKA)
print(Ans)


Fixed submission:
N=int(input())
P=[None]*(N+1)
for i in range(1,N+1):
    P[i]=int(input())

#最大長の「一つずつ増加する部分列」を求めればよい

Q=[0]*(N+2)

for i in range(1,N+1):
    Q[P[i]]=i

count=0
Ans=0
for i in range(1,N+2):
    if Q[i]>Q[i-1]:
        count+=1
    else:
        Ans=max(count,Ans)
        count=1

print(N-Ans)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
P=[None]*N
for i in range(N):
    P[i]=int(input())

#最大長の「一つずつ増加する部分列」を求めればよい

ZOUKA=[0]*(N+1)

for i in range(1,N+1):
    if ZOUKA[i]==-1:
        continue
    
    r=i
    for j in range(N):
        if P[j]==r:
            r+=1
            ZOUKA[i]+=1
            break

    for k in range(j+1,N):
        if P[k]==r:
            ZOUKA[r]=-1
            r+=1
            ZOUKA[i]+=1

Ans=N-max(ZOUKA)
print(Ans)

Fixed submission:
N=int(input())
P=[None]*(N+1)
for i in range(1,N+1):
    P[i]=int(input())

#最大長の「一つずつ増加する部分列」を求めればよい

Q=[0]*(N+2)

for i in range(1,N+1):
    Q[P[i]]=i

count=0
Ans=0
for i in range(1,N+2):
    if Q[i]>Q[i-1]:
        count+=1
    else:
        Ans=max(count,Ans)
        count=1

print(N-Ans)

Buggy python submission (verdict: Runtime Error):
N=int(input())
P=[None]*N
for i in range(N):
    P[i]=int(input())

#最大長の「一つずつ増加する部分列」を求めればよい

ZOUKA=[0]*(N)
JUDGE=list(range(1,N+1))

for i in JUDGE:
    r=i
    for j in range(N):
        if P[j]==r:
            r+=1
            ZOUKA[i]+=1
            break

    for k in range(j+1,N):
        if P[k]==r:
            JUDGE.remove(r)
            r+=1
            ZOUKA[i]+=1

Ans=N-max(ZOUKA)
print(Ans)

Fixed submission:
N=int(input())
P=[None]*(N+1)
for i in range(1,N+1):
    P[i]=int(input())

#最大長の「一つずつ増加する部分列」を求めればよい

Q=[0]*(N+2)

for i in range(1,N+1):
    Q[P[i]]=i

count=0
Ans=0
for i in range(1,N+2):
    if Q[i]>Q[i-1]:
        count+=1
    else:
        Ans=max(count,Ans)
        count=1

print(N-Ans)
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())

mod=10**9+7

FACT=[1]
for i in range(1,10**6+1):
    FACT.append(FACT[-1]*i%mod)

FACT_INV=[pow(FACT[-1],mod-2,mod)]
for i in range(10**6,0,-1):
    FACT_INV.append(FACT_INV[-1]*i%mod)

FACT_INV.reverse()

def Combi(a,b):
    if 0<=b<=a:
        return FACT[a]*FACT_INV[b]*FACT_INV[a-b]%mod
    else:
        return 0

ANS=0
MINUS=0

def k_perm(k):
    return Combi(k-1,N-k-1)*FACT[k]*FACT[N-k-1]%mod

for k in range(N):
    ANS=(ANS+(k_perm(k)-k_perm(k-1))*k)%mod

print(ANS)

Fixed submission:
N=int(input())

mod=10**9+7

FACT=[1]
for i in range(1,10**6+1):
    FACT.append(FACT[-1]*i%mod)

FACT_INV=[pow(FACT[-1],mod-2,mod)]
for i in range(10**6,0,-1):
    FACT_INV.append(FACT_INV[-1]*i%mod)

FACT_INV.reverse()

def Combi(a,b):
    if 0<=b<=a:
        return FACT[a]*FACT_INV[b]*FACT_INV[a-b]%mod
    else:
        return 0

ANS=0
MINUS=0

def k_perm(k):
    return Combi(k-1,N-k-1)*FACT[k]*FACT[N-k-1]%mod

for k in range(N):
    ANS=(ANS+(k_perm(k)-k_perm(k-1))*k)%mod

print(ANS)
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
A=list(map(int,input().split()))

X=sorted(A)

maxim=X[n-1]

if maxim%2==0:
    half=maxim//2
    target=half
    i=0
    while True:
        if (target+i in X)==1 and target+i != maxim:
            print(maxim,end=" ")
            print(target+i)
            break
        elif (target-i in X)==1 and target-i != maxim:
            print(maxim,end=" ")
            print(target-i)
            break
        else:
            i=i+1
#            print(i)

elif maxim%2==1:
    half=maxim//2
    target=half
    i=0
    while True:
        if (target+i+1 in X)==1 and target+i+1 != maxim:
            print(maxim,end=" ")
            print(target+i+1)
            break
        elif (target-i in X)==1 and target-i != maxim:
            print(maxim,end=" ")
            print(target-i)
            break
        else:
            i=i+1
#            print(i)

Fixed submission:
n=int(input())
A=list(map(int,input().split()))

maximal=max(A)
print(maximal,end=" ")
B=list(map(lambda x:abs(x - maximal/2),A))
minimam=B.index(min(B))
if A[minimam]==maximal:
    print("0")
else:
    print(A[minimam])
Buggy python submission (verdict: Time Limit Exceeded):
A,B,K=map(int,input().split())
for i in range(A,B+1):
    if i<A+K or i>B-K:
        print(i)


Fixed submission:
A,B,K=map(int,input().split())
ANS=[]
for i in range(A,A+K):
    if i<=B:
        ANS.append(i)

for i in range(B,B-K,-1):
    if i>=A:
        ANS.append(i)

for i in sorted(list(set(ANS))):
    print(i)
Buggy python submission (verdict: Runtime Error):
H,W=map(int,input().split())
MAP=[list(input()) for i in range(H)]


ANS=0
for i in range(H):
    ANS+=MAP[i].count(".")

from collections import deque

QUE = deque()
QUE.append((0,0))
MAP[0][0]=0

while QUE:
    (x,y)=QUE.pop()

    for i,j in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:
        if i<0 or j<0 or i>=H or j>=W:
            continue
        if MAP[i][j]==".":
            MAP[i][j]=MAP[x][y]+1
            QUE.appendleft((i,j))
            
        
if MAP[i][j]==".":
    print(-1)
else:
    print(ANS-MAP[H-1][W-1]-1)

Fixed submission:
H,W=map(int,input().split())
MAP=[list(input()) for i in range(H)]


ANS=0
for i in range(H):
    ANS+=MAP[i].count(".")

from collections import deque

QUE = deque()
QUE.append((0,0))
MAP[0][0]=0

while QUE:
    (x,y)=QUE.pop()

    for i,j in [(x+1,y),(x-1,y),(x,y+1),(x,y-1)]:
        if i<0 or j<0 or i>=H or j>=W:
            continue
        if MAP[i][j]==".":
            MAP[i][j]=MAP[x][y]+1
            QUE.appendleft((i,j))
            
        
if MAP[H-1][W-1]==".":
    print(-1)
else:
    print(ANS-MAP[H-1][W-1]-1)
Buggy python submission (verdict: Time Limit Exceeded):
N,K=map(int,input().split())
XYC=[list(input().split()) for i in range(N)]

MAP=[[0 for i in range(K*2+1)] for j in range(K*2+1)]
mod=K*2
for x,y,c in XYC:
    x=int(x)
    y=int(y)
    if c=="W":
        x+=K

    MAP[x%mod+1][y%mod+1]+=1
    MAP[(x+K)%mod+1][(y+K)%mod+1]+=1




for i in range(K*2+1):
    for j in range(1,K*2+1):
        MAP[i][j]=MAP[i][j-1]+MAP[i][j]

for i in range(K*2+1):
    for j in range(1,K*2+1):
        MAP[j][i]=MAP[j-1][i]+MAP[j][i]

ANS=0
for i in range(K):
    for j in range(K):

        if ANS<MAP[i+K][j+K]-MAP[i][j+K]-MAP[i+K][j]+MAP[i][j]:
            ANS=MAP[i+K][j+K]-MAP[i][j+K]-MAP[i+K][j]+MAP[i][j]
        if ANS<N-(MAP[i+K][j+K]-MAP[i][j+K]-MAP[i+K][j]+MAP[i][j]):
            ANS=N-(MAP[i+K][j+K]-MAP[i][j+K]-MAP[i+K][j]+MAP[i][j])
print(ANS)


Fixed submission:
N,K=map(int,input().split())
XYC=[list(input().split()) for i in range(N)]

MAP=[[0 for i in range(K*2+1)] for j in range(K*2+1)]
mod=K*2
for x,y,c in XYC:
    x=int(x)
    y=int(y)
    if c=="W":
        x+=K

    MAP[x%mod+1][y%mod+1]+=1
    MAP[(x+K)%mod+1][(y+K)%mod+1]+=1




for i in range(K*2+1):
    for j in range(1,K*2+1):
        MAP[i][j]=MAP[i][j-1]+MAP[i][j]

for i in range(K*2+1):
    for j in range(1,K*2+1):
        MAP[j][i]=MAP[j-1][i]+MAP[j][i]

ANS=0
for i in range(K):
    for j in range(K):

        if ANS<MAP[i+K][j+K]-MAP[i][j+K]-MAP[i+K][j]+MAP[i][j]:
            ANS=MAP[i+K][j+K]-MAP[i][j+K]-MAP[i+K][j]+MAP[i][j]
        if ANS<N-(MAP[i+K][j+K]-MAP[i][j+K]-MAP[i+K][j]+MAP[i][j]):
            ANS=N-(MAP[i+K][j+K]-MAP[i][j+K]-MAP[i+K][j]+MAP[i][j])
print(ANS)
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=[None]*N

for i in range(N):
    A[i]=int(input())

B=[None]
Ans=0
for i in A:
    if i in B:
        continue
    else:
        if A.count(i)%2==1:
            Ans+=1
            B+=[i]



print(Ans)


Fixed submission:
N=int(input())
A=[None]*N

for i in range(N):
    A[i]=int(input())

Ans=0
A.sort()
A+=[-1]
j=1
for i in range(1,N+1):
    if A[i]==A[i-1]:
        j+=1
    else:
        if j%2==1:
            Ans+=1
        j=1

print(Ans)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=[None]*N

for i in range(N):
    A[i]=int(input())

X=set(A)

Ans=0
for i in X:
    if A.count(i)%2==1:
        Ans+=1

print(Ans)


Fixed submission:
N=int(input())
A=[None]*N

for i in range(N):
    A[i]=int(input())

Ans=0
A.sort()
A+=[-1]
j=1
for i in range(1,N+1):
    if A[i]==A[i-1]:
        j+=1
    else:
        if j%2==1:
            Ans+=1
        j=1

print(Ans)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=[None]*N

for i in range(N):
    A[i]=int(input())

X=set(A)

Ans=0
for i in X:
    if A.count(i)%2==1:
        Ans+=1

print(Ans)


Fixed submission:
N=int(input())
A=[None]*N

for i in range(N):
    A[i]=int(input())

Ans=0
A.sort()
A+=[-1]
j=1
for i in range(1,N+1):
    if A[i]==A[i-1]:
        j+=1
    else:
        if j%2==1:
            Ans+=1
        j=1

print(Ans)
Buggy python submission (verdict: Runtime Error):
N,MA,MB=map(int,input().split())
S=[list(map(int,input().split())) for i in range(N)]

DPLIST=[[10**6 for i in range(401)] for j in range(401)]
DPLIST[0][0]=0

for a,b,c in S:
    for i in range(400,a-1,-1):
        for j in range(400,b-1,-1):
            if DPLIST[i][j]>DPLIST[i-a][j-b]+c:
                DPLIST[i][j]=DPLIST[i-a][j-b]+c
                
ANS=10**6
for k in range(1,400//max(i,j)):
    if DPLIST[MA*k][MB*k]<ANS:
        ANS=DPLIST[MA*k][MB*k]

if ANS==10**6:
    print(-1)
else:
    print(ANS)

Fixed submission:
N,MA,MB=map(int,input().split())
S=[list(map(int,input().split())) for i in range(N)]

DPLIST=[[10**6 for i in range(401)] for j in range(401)]
DPLIST[0][0]=0

for a,b,c in S:
    for i in range(400,a-1,-1):
        for j in range(400,b-1,-1):
            if DPLIST[i][j]>DPLIST[i-a][j-b]+c:
                DPLIST[i][j]=DPLIST[i-a][j-b]+c
                
ANS=10**6
for k in range(1,400//max(MA,MB)):
    if DPLIST[MA*k][MB*k]<ANS:
        ANS=DPLIST[MA*k][MB*k]

if ANS==10**6:
    print(-1)
else:
    print(ANS)
Buggy python submission (verdict: Time Limit Exceeded):
N,T=map(int,input().split())
A=list(map(int,input().split()))

MAX=[A[-1]]

for i in range(N-2,-1,-1):
    MAX.append(max(MAX[-1],A[i]))

MAX=MAX[::-1]
SABUN=[MAX[i]-A[i] for i in range(N)]

#print(MAX)
#print(SABUN)
MAXSA=max(SABUN)

SEG=[]
for i in range(N):
    if SABUN[i]==MAXSA:
        SEG.append(i)

SE=[SEG[0]]
for i in range(1,len(SEG)):
    if A[SEG[i]]!=A[SEG[i-1]]:
        SE.append(SEG[i])
    
SE.append(N)
#print(SEG)
#print(SE)

ANS=0
for i in range(1,len(SE)):
    x=SE[i-1]
    y=SE[i]

    ANSS=float("inf")
    for l in range(x+1,y):

        count1=A[x:l].count(A[x])
        if count1==0:
            continue

        for j in range(l,y):
            if A[j]==A[x]:
                count2=A[j:y].count(A[x]+MAXSA)

                if ANSS>count1+count2:
                    ANSS=count1+count2
                break

        else:
            if A[x]+MAXSA in A[x:l]:
                continue
            else:
                count3=A[l:y].count(A[x]+MAXSA)

                if ANSS>min(count1,count3):
                    ANSS=min(count1,count3)

        #print(l,ANSS,count1,count2)

    #print(ANSS)

    ANS+=ANSS

print(ANS)

Fixed submission:
N,T=map(int,input().split())
A=list(map(int,input().split()))

MAX=[A[-1]]

for i in range(N-2,-1,-1):
    MAX.append(max(MAX[-1],A[i]))

MAX=MAX[::-1]
SABUN=[MAX[i]-A[i] for i in range(N)]

#print(MAX)
#print(SABUN)
MAXSA=max(SABUN)

ANS=0
for i in range(N):
    if SABUN[i]==MAXSA:
        ANS+=1

print(ANS)

Buggy python submission (verdict: Time Limit Exceeded):
N,T=map(int,input().split())
A=list(map(int,input().split()))

MAX=[A[-1]]

for i in range(N-2,-1,-1):
    MAX.append(max(MAX[-1],A[i]))

MAX=MAX[::-1]
SABUN=[MAX[i]-A[i] for i in range(N)]

#print(MAX)
#print(SABUN)
MAXSA=max(SABUN)

SEG=[]
for i in range(N):
    if SABUN[i]==MAXSA:
        SEG.append(i)

SE=[SEG[0]]
for i in range(1,len(SEG)):
    if A[SEG[i]]!=A[SEG[i-1]]:
        SE.append(SEG[i])
    
SE.append(N)
#print(SEG)
#print(SE)

ANS=0
for i in range(1,len(SE)):
    x=SE[i-1]
    y=SE[i]

    ANSS=float("inf")
    for l in range(x+1,y):

        count1=A[x:l].count(A[x])
        if count1==0:
            continue

        for j in range(l,y):
            if A[j]==A[x]:
                count2=A[j:y].count(A[x]+MAXSA)

                if ANSS>count1+count2:
                    ANSS=count1+count2
                break

        else:
            if A[x]+MAXSA in A[x:l]:
                continue
            else:
                count3=A[l:y].count(A[x]+MAXSA)

                if ANSS>min(count1,count3):
                    ANSS=min(count1,count3)

        #print(l,ANSS,count1,count2)

    #print(ANSS)

    ANS+=ANSS

print(ANS)

Fixed submission:
N,T=map(int,input().split())
A=list(map(int,input().split()))

MAX=[A[-1]]

for i in range(N-2,-1,-1):
    MAX.append(max(MAX[-1],A[i]))

MAX=MAX[::-1]
SABUN=[MAX[i]-A[i] for i in range(N)]

#print(MAX)
#print(SABUN)
MAXSA=max(SABUN)

ANS=0
for i in range(N):
    if SABUN[i]==MAXSA:
        ANS+=1

print(ANS)
Buggy python submission (verdict: Time Limit Exceeded):
def dfs():
    pass


def main():
    n,k = map(int, input().split())
    dl = []
    for _ in range(k):
        l,r = map(int, input().split())
        for i in range(l,r+1):
            dl.append(i)

    dl.sort()
    MOD = 998244353
    dp = [0]*(n+1)
    dp[1] = 1
    for i in range(n):
        for d in dl:
            if i+d > n:break
            dp[i+d] += dp[i]
            dp[i+d]%=MOD

    print(dp[n])



if __name__ == "__main__":
    main()

Fixed submission:
n,k = map(int, input().split())

lrs = []
for _ in range(k):
    l,r = map(int, input().split())
    lrs.append((l,r))

dp = [0]*(n+1)
sdp = [0]*(n+1)
dp[1] = 1
sdp[1] = 1

MOD = 998244353
for i in range(2,n+1):
    # print('------',i)
    for l,r in lrs:
        dr = sdp[i-l] if i-l >= 0 else 0
        dl = sdp[i-r-1] if i-r-1 >= 0 else 0
        # print(dl,dr,'--')
        dp[i] += (dr-dl)
        dp[i] %= MOD        
    sdp[i] = sdp[i-1] + dp[i]
    sdp[i]%=MOD

# print(dp)
print(dp[n])
Buggy python submission (verdict: Runtime Error):
a,b,c,d = map(int, input().split())

if b>=0 and d>= 0:
    ans = b*d
elif b < 0 and d >= 0:
    if c <= 0: ans = max(ans,0)
    else: ans = b*c
elif b >= 0 and d < 0:
    if a <= 0: ans = 0
    else: ans = a*d
else:
    ans = a*c

if a <= 0 and c <= 0:
    ans = max(ans,a*c)

print(ans)

Fixed submission:
a,b,c,d = map(int, input().split())

ans = max(a*c,a*d,b*c,b*d)
if a*b <= 0 or c*d <= 0:
    ans = max(0,ans)

print(ans)

# if b>=0 and d>= 0:
#     ans = b*d
# elif b < 0 and d >= 0:
#     if c <= 0: ans = max(ans,0)
#     else: ans = b*c
# elif b >= 0 and d < 0:
#     if a <= 0: ans = 0
#     else: ans = a*d
# else:
#     ans = a*c

# if a <= 0 and c <= 0:
#     ans = max(ans,a*c)

# print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def primes(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    return [i for i in range(n + 1) if is_prime[i]]


def main():
    n = int(input())
    al = list(map(int, input().split()))
    ps = primes(n)
    pset = set()

    no_flag = False
    for a in al:
        for p in ps:
            if a%p == 0:
                if p in pset:
                    no_flag = True
                    break
                else:
                    pset.add(p)
        if no_flag:
            break
    else:
        print('pairwise coprime')
        exit()

    pset = set()
    for p in ps:
        if al[0]%p == 0:
            pset.add(p)

    for a in al[1:]:
        apset = set()
        for p in ps:
            if a%p == 0:
                apset.add(p)
        pset = pset&apset
        if len(pset) == 0:
            print('setwise coprime')
            break
    else:
        print('not coprime')



if __name__ == "__main__":
    main()

Fixed submission:
from math import gcd

def primes(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    return [i for i in range(n + 1) if is_prime[i]]


def main():
    n = int(input())
    al = list(map(int, input().split()))
    als = [0]*(10**6+1)
    for a in al:
        als[a] += 1

    ps = primes(10**6)
    for p in ps:
        cnt = 0
        for i in range(p,10**6+1,p):
            cnt += als[i]
        if cnt >= 2:
            break
    else:
        print('pairwise coprime')
        exit() 

    c_gcd = al[0]
    for a in al[1:]:
        c_gcd = gcd(c_gcd,a)
    
    if c_gcd == 1:
        print('setwise coprime')
    else:
        print('not coprime')


    # no_flag = False
    # for a in al:
    #     for p in ps:
    #         if a%p == 0:
    #             if p in pset:
    #                 no_flag = True
    #                 break
    #             else:
    #                 pset.add(p)
    #     if no_flag:
    #         break
    # else:
    #     print('pairwise coprime')
    #     exit()

    # pset = set()
    # for p in ps:
    #     if al[0]%p == 0:
    #         pset.add(p)

    # for a in al[1:]:
    #     apset = set()
    #     for p in ps:
    #         if a%p == 0:
    #             apset.add(p)
    #     pset = pset&apset
    #     if len(pset) == 0:
    #         print('setwise coprime')
    #         break
    # else:
    #     print('not coprime')



if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
vn = int(input())
ll = list(map(int, input().split()))
cnt = 0
for i in range(n):
    for j in range(i+1,n):
        for k in range(j+1,n):
            a,b,c = ll[i], ll[j], ll[k]
            if a == b or b == c or c == a:
                continue
            vs = [a,b,c]
            vs.sort()
            if vs[0] + vs[1] > vs[2]: 
                cnt += 1
                # print(vs)

print(cnt)


Fixed submission:
n = int(input())
ll = list(map(int, input().split()))
cnt = 0
for i in range(n):
    for j in range(i+1,n):
        for k in range(j+1,n):
            a,b,c = ll[i], ll[j], ll[k]
            if a == b or b == c or c == a:
                continue
            vs = [a,b,c]
            vs.sort()
            if vs[0] + vs[1] > vs[2]: 
                cnt += 1
                # print(vs)

print(cnt)
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
input().rstrip() # remove last '\n'

def main():
    r,c,k = map(int, input().split())
    rcv = [ [0]*c for _ in range(r) ]

    for _ in range(k):
        ri,ci,vi = map(int, input().split())
        rcv[ri-1][ci-1] = vi

    # dp[0] = [ [0]*c for _ in range(r) ]
    # dp1 = [ [0]*c for _ in range(r) ]
    # dp2 = [ [0]*c for _ in range(r) ]
    # dp[3] = [ [0]*c for _ in range(r) ]
    dp = [ [ [0]*c  for _ in range(r) ] for _ in range(4) ]


    dp[1][0][0] = rcv[0][0]
    for i in range(r):
        for j in range(c):
            if i+1 < r:
                dp[0][i+1][j] = max(dp[0][i][j], dp[1][i][j], dp[2][i][j], dp[3][i][j], dp[0][i+1][j])
                if rcv[i+1][j] > 0:
                    dp[1][i+1][j] = max(dp[1][i+1][j],  max(dp[0][i][j], dp[1][i][j], dp[2][i][j], dp[3][i][j])+rcv[i+1][j])

            if j+1 < c:
                dp[0][i][j+1] = max(dp[0][i][j], dp[0][i][j+1])
                dp[1][i][j+1] = max(dp[1][i][j], dp[1][i][j+1])
                dp[2][i][j+1] = max(dp[2][i][j], dp[2][i][j+1])
                dp[3][i][j+1] = max(dp[3][i][j], dp[3][i][j+1])
                if rcv[i][j+1] > 0:
                    dp[1][i][j+1] = max(dp[1][i][j+1], dp[0][i][j]+rcv[i][j+1])
                    dp[2][i][j+1] = max(dp[2][i][j+1], dp[1][i][j]+rcv[i][j+1])
                    dp[3][i][j+1] = max(dp[3][i][j+1], dp[2][i][j]+rcv[i][j+1])

    ans = max(dp[0][r-1][c-1], dp[1][r-1][c-1], dp[2][r-1][c-1], dp[3][r-1][c-1])
    print(ans)


if __name__ == "__main__":
    main()

Fixed submission:
r,c,k = map(int, input().split())
rcv = [ [0]*c for _ in range(r) ]

for _ in range(k):
    ri,ci,vi = map(int, input().split())
    rcv[ri-1][ci-1] = vi

dp0 = [ [0]*c for _ in range(r) ]
dp1 = [ [0]*c for _ in range(r) ]
dp2 = [ [0]*c for _ in range(r) ]
dp3 = [ [0]*c for _ in range(r) ]

dp1[0][0] = rcv[0][0]
for i in range(r):
    for j in range(c):
        if i+1 < r:
            dp0[i+1][j] = max(dp0[i][j], dp1[i][j], dp2[i][j], dp3[i][j], dp0[i+1][j])
            if rcv[i+1][j] > 0:
                dp1[i+1][j] = max(dp1[i+1][j],  max(dp0[i][j], dp1[i][j], dp2[i][j], dp3[i][j])+rcv[i+1][j])

        if j+1 < c:
            dp0[i][j+1] = max(dp0[i][j], dp0[i][j+1])
            dp1[i][j+1] = max(dp1[i][j], dp1[i][j+1])
            dp2[i][j+1] = max(dp2[i][j], dp2[i][j+1])
            dp3[i][j+1] = max(dp3[i][j], dp3[i][j+1])
            if rcv[i][j+1] > 0:
                dp1[i][j+1] = max(dp1[i][j+1], dp0[i][j]+rcv[i][j+1])
                dp2[i][j+1] = max(dp2[i][j+1], dp1[i][j]+rcv[i][j+1])
                dp3[i][j+1] = max(dp3[i][j+1], dp2[i][j]+rcv[i][j+1])

ans = max(dp0[r-1][c-1], dp1[r-1][c-1], dp2[r-1][c-1], dp3[r-1][c-1])
print(ans)
# print(dp0)
# print(dp1)
# print(dp2)
# print(dp3)
Buggy python submission (verdict: Runtime Error):
from collections import deque
from bisect import bisect_left, bisect_right
from itertools import accumulate


n,m,k = map(int, input().split())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))

al_sums = accumulate(al)
bl_sums = accumulate(bl)

ans = 0
for i in range(0,n+1):
    if i == 0: 
        a_sum = 0
    else:
        a_sum = al_sums[i-1]
    if a_sum > k: 
        continue
    cnt = bisect_right(bl_sums, k-a_sum)
    curr_ans = i+cnt
    ans = max(ans,curr_ans)

print(ans)

Fixed submission:
from collections import deque
from bisect import bisect_left, bisect_right

n,m,k = map(int, input().split())

al = list(map(int, input().split()))
bl = list(map(int, input().split()))

al_sums = [0]*(n+1)
bl_sums = [0]*(m)

curr_sum = 0
for i,a in enumerate(al):
    curr_sum += a
    al_sums[i+1] = curr_sum

curr_sum = 0
for i,b in enumerate(bl):
    curr_sum += b
    bl_sums[i] = curr_sum

# print(al_sums)
# print(bl_sums)


ans = 0
for i in range(n+1):
    a_sum = al_sums[i]
    if a_sum > k: continue
    cnt = bisect_right(bl_sums, k-a_sum)
    curr_ans = i+cnt
    # print(i,curr_ans, k-a_sum)
    ans = max(ans,curr_ans)

print(ans)