Problem p02623 (python)

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

best = 0

a, b = [0], [0]
for i in range(N):
    a.append(a[i] + A[i])
for j in range(M):
    b.append(b[j] + B[j])

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):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

if k < a[0] and k < b[0]:
    print(0)
    exit()

if k < a[0] and k >= b[0]:
    for i in range(len(b)):
        k -= b[i]
        if k < 0:
            print(i)
            exit()
        if i == len(b)-1:
            print(m)
            exit()

if k >= a[0] and k < b[0]:
    for i in range(len(a)):
        k -= a[i]
        if k < 0:
            print(i)
            exit()
        if i == len(a)-1:
            print(n)
            exit()

i = 0
j = 0
c = []

while True:
    if a[i] <= b[j]:
        c.append(a[i])
        i += 1
        if i == n -1:
            if j == m -1:
                break
            else:
                for l in range(j, m):
                    c.append(b[l])
                break
    else:
        c.append(b[j])
        j += 1
        if j == m -1:
            if i == n -1:
                break
            else:
                for l in range(i, m):
                    c.append(a[l])
                break

for i in range(len(c)):
    k -= c[i]
    if k < 0:
        print(i)
        exit()
    if i == len(c)-1:
        print(len(c))
        exit()

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 k < a[0] and k < b[0]:
    print(0)
    exit()

if k < a[0] and k >= b[0]:
    for i in range(len(b)):
        k -= b[i]
        if k < 0:
            print(i)
            exit()
        if i == len(b)-1:
            print(m)
            exit()

if k >= a[0] and k < b[0]:
    for i in range(len(a)):
        k -= a[i]
        if k < 0:
            print(i)
            exit()
        if i == len(a)-1:
            print(n)
            exit()

i = 0
j = 0
c = []

while True:
    if a[i] <= b[j]:
        c.append(a[i])
        i += 1
        if i == n -1:
            if j == m -1:
                break
            else:
                for l in range(j, m):
                    c.append(b[l])
                break
    else:
        c.append(b[j])
        j += 1
        if j == m -1:
            if i == n -1:
                break
            else:
                for l in range(i, m):
                    c.append(a[l])
                break

for i in range(len(c)):
    k -= c[i]
    if k < 0:
        print(i)
        exit()
    if i == len(c)-1:
        print(n+m)
        exit()

Buggy submission (verdict: Runtime Error):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

i = 0
j = 0
c = []

if k < a[0] and k < b[0]:
    print(0)
    exit()

if k < a[0] and k >= b[0]:
    for i in range(len(b)):
        k -= b[i]
        if k < 0:
            print(i)
            exit()
        if i == len(b)-1:
            print(m)
            exit()

if k >= a[0] and k < b[0]:
    for i in range(len(a)):
        k -= a[i]
        if k < 0:
            print(i)
            exit()
        if i == len(a)-1:
            print(n)
            exit()


while True:
    if a[i] <= b[j]:
        c.append(a[i])
        i += 1
        if i == n -1:
            if j == m -1:
                break
            else:
                for l in range(j, m):
                    c.append(b[l])
                break
    else:
        c.append(b[j])
        j += 1
        if j == m -1:
            if i == n -1:
                break
            else:
                for l in range(i, m):
                    c.append(a[l])
                break

for i in range(len(c)):
    k -= c[i]
    if k < 0:
        print(i)
        exit()
    if i == len(c)-1:
        print(n+m)
        exit()

Buggy submission (verdict: Runtime Error):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

i = 0
j = 0
c = []

if k < a[0] and k < b[0]:
    print(0)
    exit()

while True:
    if a[i] <= b[j]:
        c.append(a[i])
        i += 1
        if i == n -1:
            if j == m -1:
                break
            else:
                for l in range(j, m):
                    c.append(b[l])
                break
    else:
        c.append(b[j])
        j += 1
        if j == m -1:
            if i == n -1:
                break
            else:
                for l in range(i, m):
                    c.append(a[l])
                break

for i in range(len(c)):
    k -= c[i]
    if k < 0:
        print(i)
        exit()
    if i == len(c)-1:
        print(n+m)
        exit()