Problem p02623 (python)

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

ruiA, ruiB = [0], [0]

for i in range(N):
    ruiA.append(ruiA[i] + A[i])

for j in range(M):
    ruiB.append(ruiB[j] + B[j])

t = False
for i in range(N + M + 1):
    if i <= N and ruiA[i] <= K:
        continue
    if i <= M and ruiB[i] <= K:
        continue
    if i > N and ruiA[N] + ruiB[i - N] <= K:
        continue
    if i > M and ruiA[i - M] + ruiB[M] <= K:
        continue

    t = False
    for j in range(0, i + 1):
        if j > M or i - j > N:
            continue
        if ruiA[i - j] + ruiB[j] <= K:
            t = True
            break

    if not t:
        print(i - 1)
        exit()

print(N + M)

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

ruiA, ruiB = [0], [0]

for i in range(N):
    ruiA.append(ruiA[i] + A[i])

for j in range(M):
    ruiB.append(ruiB[j] + B[j])

tmpa, tmpb = 0, 0
tmp = 0
t = False
for i in range(N + M + 1):
    if i <= N and ruiA[i] <= K:
        continue
    if i <= M and ruiB[i] <= K:
        continue
    if ruiA[N] + ruiB[i - N] <= K:
        continue
    if ruiA[i - M] + ruiB[M] <= K:
        continue

    t = False
    for j in range(0, i + 1):
        if j > M or i - j > N:
            continue
        if ruiA[i - j] + ruiB[j] <= K:
            t = True
            break

    if not t:
        print(i - 1)
        exit()

print(N + M)

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

ruiA, ruiB = [0], [0]

for i in range(N):
    ruiA.append(ruiA[i] + A[i])

for j in range(M):
    ruiB.append(ruiB[j] + B[j])

tmpa, tmpb = 0, 0
tmp = 0
t = False
for i in range(N + M + 1):
    if i <= N and ruiA[i] <= K:
        continue
    if i <= M and ruiB[i] <= K:
        continue
    if ruiA[N] + ruiB[i - N] <= K:
        continue
    if ruiA[i - M] + ruiB[M] <= K:
        continue
    
    t = False
    for j in range(max(0, i - N), min(i, M)):
        if ruiA[i - j] + ruiB[j] <= K:
            t = True
            break

    if not t:
        print(i - 1)
        exit()

print(N + M)

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

if sum(A) + sum(B) <= K:
    print(len(A) + len(B))
    exit()

a, b = 0, 0
while True:
    if a == N:
        K -= B[b]
        b += 1
    
    elif b == M:
        K -= a[a]
        a += 1

    elif A[a] < B[b]:
        K -= A[a]
        a += 1
    
    elif A[a] > B[b]:
        K -= B[b]
        b += 1

    else:
        tmpa, tmpb = a, b
        while True:
            tmpa += 1
            tmpb += 1
            if tmpa == N:
                K -= B[b]
                b += 1
                break
            elif tmpb == M:
                K -= A[a]
                a += 1
                break
            elif A[tmpa] > B[tmpb]:
                K -= B[b]
                b += 1
                break
            elif A[tmpa] < B[tmpb]:
                K -= A[a]
                a += 1
                break 
    if K < 0:
        break

print(a + b - 1)