Problem p02623 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
# from collections import deque
from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np
# from numpy import cumsum  # accumulate
from bisect import bisect_right, bisect_left

def solve():
    N, M, K = MI()
    A = LI()
    B = LI()
    A = [0] + list(accumulate(A))
    B = [0] + list(accumulate(B))

    def is_ok(arg, a):
        # b <= K - a
        # print(arg, B[arg], a, K, B[arg] <= K - a)
        return B[arg] <= K - a

    def meguru_bisect(ng, ok, a):
        while (abs(ok - ng) > 1):
            mid = (ok + ng) // 2
            if is_ok(mid, a):
                ok = mid
            else:
                ng = mid
            # print(ok, ng)
        return ok

    # print(A)
    # print(B)
    ans = 0
    for i, a in enumerate(A):
        idx = meguru_bisect(M+1, -1, a)
        if 0 <= idx <= M:
            ans = max(ans, i + idx)
    print(ans)

        



if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
# from collections import deque
from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np
# from numpy import cumsum  # accumulate

def solve():
    N, M, K = MI()
    A = LI()
    B = LI()
    
    # print(list(accumulate(A)))
    # print(list(accumulate(B)))
    ra = list(accumulate(A))[::-1]
    rb = list(accumulate(B))[::-1]
    na = N
    for a in ra:
        if a > K:
            na = na - 1
        else:
            break
    nb = M
    for b in rb:
        if b > K:
            nb = nb -1
        else:
            break
    # print(na, nb)
    if na == 0 and nb == 0:
        print(0)
        return
    elif na == 0:
        print(nb)
        return
    elif nb == 0:
        print(na)
        return

    ra = ra[-na:]
    rb = rb[-nb:]
    # print(ra)
    # print(rb)
    ia = 0
    ib = 0
    while ra[ia] + rb[ib] > K:
        if ra[ia] > rb[ib]:
            ia = ia + 1
            na = na - 1
        else:
            ib = ib + 1
            nb = nb - 1
    print(na + nb)


    
if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
from collections import deque
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np
# from numpy import cumsum  # accumulate

def solve():
    N, M, K = MI()
    A = deque(LI())
    B = deque(LI())

    ans = 0
    sm = 0
    while 1:
        #print(A, B, sm)
        if A and B:
            a = A.popleft()
            b = B.popleft()
            if a < b:
                if a + sm <= K:
                    sm = sm + a
                    ans += 1
                    B.appendleft(b)
                else:
                    break
            elif b < a:
                if b + sm <= K:
                    sm = sm + b
                    ans += 1
                    A.appendleft(a)
                else:
                    break
            else:   # a == b
                if a + sm > K: break

                for a2, b2 in zip(A, B):
                    if a2 == b2: continue
                    elif a2 < b2:
                        sm = sm + a
                        ans += 1
                        B.appendleft(b)
                    elif b2 < a2:
                        sm = sm + b
                        ans += 1
                        A.appendleft(a)
        elif A:
            a = A.popleft()
            if sm + a <= K:
                sm = sm + a
                ans += 1
            else:
                break
        elif B:
            b = B.popleft()
            if sm + b <= K:
                sm = sm + b
                ans += 1
            else:
              break
        else:
            break
    print(ans)

if __name__ == '__main__':
    solve()