Problem p02774 (python)

Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))

ap, am, a0 = [], [], []
for i in a:
    if i > 0:
        ap.append(i)
    elif i < 0:
        am.append(i)
    else:
        a0.append(i)
lp, lm, l0 = len(ap), len(am), len(a0)
ap.sort()
am.sort(reverse=True)

# binary search
if k <= lp * lm:
    start = max(ap) * min(am)
    stop = -1
    while start < stop:
        # i以下になるペアがnum個ある
        # 尺取法
        num = 0
        i = (start + stop) // 2
        j = 0
        flag = True
        for l in range(lm-1, -1, -1):
            while flag and am[l] * ap[j] > i:
                j += 1
                if j == lp:
                    flag = False
                    break
            num += lp - j
        if num < k:
            start = i + 1
        else:
            stop = i
    print(start)

elif k <= lp * lm + (lp + lm) * l0 + l0 * (l0 - 1) // 2:
    print(0)

else:
    k -= lp * lm + (lp + lm) * l0 + l0 * (l0 - 1) // 2
    start = 1
    if ap == []:
        stop = min(am) * min(am)
    elif am == []:
        stop = max(ap) * max(ap)
    else:
        stop = max([max(ap) * max(ap), min(am) * min(am)])
    while start < stop:
        # 1以上i以下になるペアがnum個ある
        # 尺取法
        num = 0
        i = (start + stop) // 2

        # 正の数
        j = 0
        flag = True
        flag2 = True
        for l in range(lp-1, -1, -1):
            while flag and ap[l] * ap[j] <= i:
                if flag2 and l <= j:
                    num -= l + 1
                    flag2 = False
                j += 1
                if j == lp:
                    flag = False
            num += j

        # 負の数
        j = 0
        flag = True
        flag2 = True
        for l in range(lm-1, -1, -1):
            while flag and am[l] * am[j] <= i:
                if flag2 and l <= j:
                    num -= l + 1
                    flag2 = False
                j += 1
                if j == lm:
                    flag = False
            num += j

        num //= 2
        if num < k:
            start = i + 1
        else:
            stop = i
    print(start)

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

ap, am, a0 = [], [], []
for i in a:
    if i > 0:
        ap.append(i)
    elif i < 0:
        am.append(i)
    else:
        a0.append(i)
lp, lm, l0 = len(ap), len(am), len(a0)
ap.sort()
am.sort(reverse=True)

# binary search
if k <= lp * lm:
    start = max(ap) * min(am)
    stop = -1
    while start < stop:
        # i以下になるペアがnum個ある
        # 尺取法
        num = 0
        i = (start + stop) // 2
        j = 0
        flag = True
        for l in range(lm-1, -1, -1):
            while flag and am[l] * ap[j] > i:
                j += 1
                if j == lp:
                    flag = False
                    break
            num += lp - j
        if num < k:
            start = i + 1
        else:
            stop = i
    print(start)

elif k <= lp * lm + (lp + lm) * l0 + l0 * (l0 - 1) // 2:
    print(0)

else:
    k -= lp * lm + (lp + lm) * l0 + l0 * (l0 - 1) // 2
    start = 1
    stop = max([max(ap) * max(ap), min(am) * min(am)])
    while start < stop:
        # 1以上i以下になるペアがnum個ある
        # 尺取法
        num = 0
        i = (start + stop) // 2

        # 正の数
        j = 0
        flag = True
        flag2 = True
        for l in range(lp-1, -1, -1):
            while flag and ap[l] * ap[j] <= i:
                if flag2 and l <= j:
                    num -= l + 1
                    flag2 = False
                j += 1
                if j == lp:
                    flag = False
            num += j

        # 負の数
        j = 0
        flag = True
        flag2 = True
        for l in range(lm-1, -1, -1):
            while flag and am[l] * am[j] <= i:
                if flag2 and l <= j:
                    num -= l + 1
                    flag2 = False
                j += 1
                if j == lm:
                    flag = False
            num += j

        num //= 2
        if num < k:
            start = i + 1
        else:
            stop = i
    print(start)