Problem p02623 (python)

Fixed submission:
import sys
import math
import heapq
from collections import defaultdict, deque
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

'''C Tsundoku'''
n,m,k=rm()
a=rl()
b=rl()
c=[0]*(n+1)
d=[0]*(m+1)
for i in range(n):
    c[i+1]=c[i]+a[i]
for i in range(m):
    d[i+1]=d[i]+b[i]
ans=0;j=m
for i in range(n+1):
    if c[i]>k:
        break
    while c[i]+d[j]>k:
        j-=1
    ans=max(ans,i+j)
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
import math
import heapq
from collections import defaultdict, deque
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

'''C Tsundoku'''
n,m,k=rm()
a=rl()
b=rl()
c=[0]*(n+1)
d=[0]*(b+1)
for i in range(n):
    c[i+1]=c[i]+a[i]
for i in range(m):
    d[i+1]=d[i]+b[i]
ans=0;j=m
for i in range(n+1):
    if c[i]>k:
        break
    while c[i]+d[j]>k:
        j-=1
    ans=max(ans,i+j)
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
import math
import heapq
from collections import defaultdict, deque
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

'''C Tsundoku'''
n,m,k=rm()
a=rl()
b=rl()
c=[0]*(n+1)
d=[0]*(b+1)
for i in range(n):
    c[i+1]=c[i]+a[i]
for i in range(m):
    d[i+1]=d[i]+b[i]
ans=0;j=m
for i in range(n+1):
    if c[i]>k:
        break
    while c[i]+c[j]>k:
        j-=1
    ans=max(ans,i+j)
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
import math
import heapq
from collections import defaultdict, deque
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

'''C Tsundoku v2
def count(i,n,j,m,a,b,k):
    if k<0:
        return -1
    if i==n and j==m:
        return 0
    if i==n:
        if b[j]<=k:
            return 1+count(i,n,j+1,m,a,b,k-b[j])
        return 0
    if j==m:
        if a[i]<=k:
            return 1+count(i+1,n,j,m,a,b,k-a[i])
        return 0
    return max(1+count(i,n,j+1,m,a,b,k-b[j]),1+count(i+1,n,j,m,a,b,k-a[i]))

n,m,k=rm()
a=rl()
b=rl()
i=0;j=0;
print(count(i,n,j,m,a,b,k))'''

'''C M1'''
n,m,k=rm()
a=rl()
b=rl()
i=0;j=0;cnt=0;cnt1=0;cnt2=0;la=n;lb=m;k1=k;k2=k
while i<la and k1>0:
    if a[i]<=k1:
        cnt1+=1
        k1-=a[i]
        i+=1
    else:
        break
i=0;j=0
while j<lb and k2>0:
    if a[j]<=k2:
        cnt2+=1
        k2-=a[j]
        j+=1
    else:
        break
i=0;j=0
while (i<la or j<lb) and k>0:
    if i==la:
        if b[j]<=k:
            cnt+=1
            k-=b[j]
            j+=1
        else:
            break
    elif j==lb:
        if a[i]<=k:
            cnt+=1
            k-=a[i]
            i+=1
        else:
            break
    elif a[i]<=b[j]:
        if a[i]<=k:
            k-=a[i]
            cnt+=1
            i+=1
        else:
            break
    else:
        if b[j]<=k:
            k-=b[j]
            cnt+=1
            j+=1
        else:
            break
print(max(cnt,cnt1,cnt2))

Buggy submission (verdict: Runtime Error):
import sys
import math
import heapq
from collections import defaultdict, deque
sys.setrecursionlimit(100000)
input = sys.stdin.readline
def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

'''C Tsundoku'''
def count(i,n,j,m,a,b,k):
    if k<0:
        return 0
    if i==n:
        if j==m:
            return 0
        if b[j]<=k:
            return 1+count(i,n,j+1,m,a,b,k-b[j])
        return 0
    elif j==m:
        if i==n:
            return 0
        if a[i]<=k:
            return 1+count(i+1,n,j,m,a,b,k-a[i])
        return 0
    else:
        if a[i]>k and b[j]>k:
            return 0
        if a[i]>k:
            return 1+count(i,n,j+1,m,a,b,k-b[j])
        if b[j]>k:
            return 1+count(i+1,n,j,m,a,b,k-a[i])
        return max(1+count(i,n,j+1,m,a,b,k-b[j]),1+count(i+1,n,j,m,a,b,k-a[i]))

n,m,k=rm()
a=rl()
b=rl()
i=0;j=0;
print(count(i,n,j,m,a,b,k))

Buggy submission (verdict: Runtime Error):
import sys
import math
import heapq
from collections import defaultdict, deque
sys.setrecursionlimit(100000) 
input = sys.stdin.readline
def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

'''C Tsundoku'''
def count(i,n,j,m,a,b,k):
    if i==n:
        if j==m:
            return 0
        if b[j]<=k:
            return 1+count(i,n,j+1,m,a,b,k-b[j])
        return 0
    elif j==m:
        if i==n:
            return 0
        if a[i]<=k:
            return 1+count(i+1,n,j,m,a,b,k-a[i])
        return 0
    else:
        if a[i]>k and b[j]>k:
            return 0
        if a[i]>k:
            return 1+count(i,n,j+1,m,a,b,k-b[j])
        if b[j]>k:
            return 1+count(i+1,n,j,m,a,b,k-a[i])
        return max(1+count(i,n,j+1,m,a,b,k-b[j]),1+count(i+1,n,j,m,a,b,k-a[i]))

n,m,k=rm()
a=rl()
b=rl()
i=0;j=0;
print(count(i,n,j,m,a,b,k))

Buggy submission (verdict: Runtime Error):
import sys
import math
import heapq
from collections import defaultdict, deque
input = sys.stdin.readline
def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

n,m,k=rm()
a=rl()
b=rl()
i=0;j=0;la=len(a);lb=len(b);cnt=0
while (i<la or j<lb) and k>0:
    if i==la:
        if b[j]<=k:
            cnt+=1
            k-=b[j]
            j+=1
        else:
            break
    elif i==lb:
        if a[i]<=k:
            cnt+=1
            k-=a[i]
            i+=1
        else:
            break
    elif a[i]<=b[j]:
        if a[i]<=k:
            k-=a[i]
            cnt+=1
            i+=1
        else:
            break
    else:
        if b[j]<=k:
            k-=b[j]
            cnt+=1
            j+=1
        else:
            break
print(cnt)