Problem p02623 (python)

Fixed submission:
from itertools import accumulate

N, M, K = map(int, input().split())
A = list(accumulate([0] + list(map(int, input().split()))))
B = list(accumulate([0] + list(map(int, input().split()))))


ans, j = 0, M

for i in range(N+1):
    if A[i] > K:
        break
    while B[j] > K-A[i]:
        j -= 1
    ans = max(ans, i+j)


print(ans)

Buggy submission (verdict: Runtime Error):
import sys #追加
sys.setrecursionlimit(500*500)

N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

time = 0
cnt = 0
ans = 0
ai = -1
bi = -1

def dfs(time, cnt):
    global ans, ai, bi
    # print(time)
    if time > K:
        # print(cnt)
        ans = max(ans, cnt-1)
        return
    if ai <= N-2:
        ai += 1
        dfs(time+A[ai], cnt+1)
    if bi+1 <= M-2:
        bi += 1
        dfs(time+B[bi]+1, cnt+1)

if sum(A)+sum(B)<=K:
    print(N+M)
else:
    dfs(0, 0)
    print(ans)

Buggy submission (verdict: Runtime Error):

N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

time = 0
cnt = 0
b = []

for i in B:
    if i+time>K:
        break
    time += i
    cnt += 1
    b.append(i)

b.reverse()
# print(b)
# print(cnt)
bi = 0
for i in range(N):
    if time+A[i] <= K:
        cnt += 1
        time += A[i]
    else:
        if len(b)-1 >= bi:
            if A[i] <= b[bi]:
                time -= b[bi]-A[i]
                bi += 1
        else:
            break
            # print("a")
            # print(time+A[i])
       
    # print(time)

time2 = 0
cnt2 = 0
a = []

for i in A:
    if i+time>K:
        break
    time2 += i
    cnt2 += 1
    a.append(i)

a.reverse()
# print(b)
# print(cnt)
ai = 0
for i in range(N):
    if time2+B[i] <= K:
        cnt2 += 1
        time2 += B[i]
    else:
        if len(a)-1 >= ai:
            if B[i] <= a[ai]:
                time2 -= a[ai]-B[i]
                ai += 1
        else:
            break
            # print("a")
            # print(time+A[i])
       
    # print(time)


print(max(cnt, cnt2))

Buggy submission (verdict: Runtime Error):

N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

time = 0
cnt = 0
b = []

for i in B:
    if i+time>K:
        break
    time += i
    cnt += 1
    b.append(i)

b.reverse()
# print(b)
# print(cnt)
bi = 0
for i in range(N):
    if time+A[i] <= K:
        cnt += 1
        time += A[i]
    else:
        if len(b)-1 >= bi:
            if A[i] <= b[bi]:
                time -= b[bi]-A[i]
                bi += 1
        else:
            break
            # print("a")
            # print(time+A[i])
       
    # print(time)

time2 = 0
cnt2 = 0
a = []

for i in A:
    if i+time>K:
        break
    time2 += i
    cnt2 += 1
    a.append(i)

a.reverse()
# print(b)
# print(cnt)
ai = 0
for i in range(N):
    if time2+B[i] <= K:
        cnt2 += 1
        time2 += B[i]
    else:
        if len(a)-1 >= ai:
            if B[i] <= a[ai]:
                time2 -= a[ai]-B[i]
                ai += 1
        else:
            break
            # print("a")
            # print(time+A[i])
       
    # print(time)


print(max((cnt, cnt2))

'''
5 3 8
5 0 0 1 1
1 2 1000
'''

Buggy submission (verdict: Runtime Error):
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

time = 0
cnt = 0
ans = 0

def dfs(time, cnt):
    global ans
    # print(time)
    if time > K:
        # print(cnt)
        ans = max(ans, cnt-1)
        return
    if A:
        dfs(time+A.pop(0), cnt+1)
    if B:
        dfs(time+B.pop(0), cnt+1)
    if len(A)==0==len(B):
        return cnt

if sum(A)+sum(B)<=K:
    print(N+M)
else:
    dfs(0, 0)
    print(ans)

Buggy submission (verdict: Runtime Error):
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

time = 0
cnt = 0
ans = 0

def dfs(time, cnt):
    global ans
    # print(time)
    if time > K:
        # print(cnt)
        ans = max(ans, cnt-1)
        return
    if A:
        dfs(time+A.pop(0), cnt+1)
    if B:
        dfs(time+B.pop(0), cnt+1)
    if len(A)==0==len(B):
        return cnt

if sum(A)+sum(B)<=K:
    print(N+M)
else:
    dfs(0, 0)
    print(ans)