Problem p02623 (python)

Fixed submission:
import sys 
import numpy as np
import bisect as bs 

def binarySearch_canread(data, target):
    if len(data) == 0:
        return None
    if len(data) == 1:
        return data[0]

    # 追加　全部読める場合と何も読めない場合
    if target > max(data):
        return len(data)
    elif target < min(data):
        return 0

    min_diff = float('inf')
    imin = 0
    imax = len(data) - 1
    closest_num = None

    while imin <= imax:
        imid = imin + (imax - imin) // 2

        if imid + 1 < len(data):
            min_diff_right = abs(data[imid + 1] - target)
        if imid > 0:
            min_diff_left = abs(data[imid - 1] - target)

        if min_diff_left < min_diff:
            min_diff = min_diff_left 
            closest_num = data[imid - 1]
        if min_diff_right < min_diff:
            min_diff = min_diff_right
            closest_num = data[imid + 1]

        if data[imid] < target:
            imin = imid + 1
        elif data[imid] > target:
            imax = imid - 1
        else:
            return list(data).index(data[imid]) + 1 # data[imid]

    # 何冊読めるかを返したい
    differ = target - closest_num
    can_read = None
    if differ > 0: # 読める時間（target）の方が大きい
        can_read = max([i for i, x in enumerate(data) if x == closest_num]) + 1
    elif differ < 0: # 読める時間の方が少ない
        can_read = min([i for i, x in enumerate(data) if x == closest_num])



    return can_read

if __name__ == '__main__':
    read = sys.stdin.buffer.read 
    readline = sys.stdin.buffer.readline
    readlines = sys.stdin.buffer.readlines 

    N, M, K = map(int, readline().split())
    A = np.array(readline().split(), np.int64)
    B = np.array(readline().split(), np.int64)

    Acum = np.zeros(N+1, np.int64)
    Acum[1:] = np.cumsum(A)
    Bcum = np.zeros(M+1, np.int64)
    Bcum[1:] = np.cumsum(B)

    # Aから1冊ずつ取っていく
    max_read = 0
    # O(N * logN)
    for i, cumNum in enumerate(Acum): # Aを1つも撮らない場合もあるので0から
        if cumNum <= K:
            limit = K - cumNum
            # O(logN)
            can_reading = i + bs.bisect_right(Bcum[1:], limit)
            if (max_read < can_reading):
                max_read = can_reading

    print(max_read)

Buggy submission (verdict: Runtime Error):
import sys 
import numpy as np

def binarySearch_canread(data, target):
    if len(data) == 0:
        return None
    if len(data) == 1:
        return data[0]

    # 追加　全部読める場合と何も読めない場合
    if target > max(data):
        return len(data)
    elif target < min(data):
        return 0

    min_diff = float('inf')
    imin = 0
    imax = len(data) - 1
    closest_num = None

    while imin <= imax:
        imid = imin + (imax - imin) // 2

        if imid + 1 < len(data):
            min_diff_right = abs(data[imid + 1] - target)
        if imid > 0:
            min_diff_left = abs(data[imid - 1] - target)

        if min_diff_left < min_diff:
            min_diff = min_diff_left 
            closest_num = data[imid - 1]
        if min_diff_right < min_diff:
            min_diff = min_diff_right
            closest_num = data[imid + 1]

        if data[imid] < target:
            imin = imid + 1
        elif data[imid] > target:
            imax = imid - 1
        else:
            return list(data).index(data[imid]) + 1 # data[imid]

    # 何冊読めるかを返したい
    differ = target - closest_num
    can_read = None
    if differ > 0: # 読める時間（target）の方が大きい
        can_read = max([i for i, x in enumerate(data) if x == closest_num]) + 1
    elif differ < 0: # 読める時間の方が少ない
        can_read = min([i for i, x in enumerate(data) if x == closest_num])



    return can_read

if __name__ == '__main__':
    read = sys.stdin.buffer.read 
    readline = sys.stdin.buffer.readline
    readlines = sys.stdin.buffer.readlines 

    N, M, K = map(int, readline().split())
    A = np.array(readline().split(), np.int64)
    B = np.array(readline().split(), np.int64)

    Acum = np.zeros(N+1, np.int64)
    Acum[1:] = np.cumsum(A)
    Bcum = np.zeros(M+1, np.int64)
    Bcum[1:] = np.cumsum(B)

    # Aから1冊ずつ取っていく
    max_read = 0
    # O(N * logN)
    for i, cumNum in enumerate(Acum): # Aを1つも撮らない場合もあるので0から
        if cumNum <= K:
            limit = K - cumNum
            # O(logN)
            can_reading = i + binarySearch_canread(Bcum[1:], limit)
            if (max_read < can_reading):
                max_read = can_reading

    print(max_read)