Problem p02774 (python)

Fixed submission:
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
p = []
z = []
m = []
for i in range(n):
    if a[i] > 0:
        p.append(a[i])
    elif a[i] == 0:
        z.append(a[i])
    else:
        m.append(a[i])
nb1 = -10**18-1
nb2 = 10**18+1
p1 = len(p)
z1 = len(z)
m1 = len(m)
#print(p,m)
#print(p1,z1,m1)
def sytr(x,p,z,m,p1,z1,m1):
    ans = 0
    ans2 = 0
    ans3 = -10**18
    if x > 0:
        m.sort(reverse=True)
        p.sort()
        ans += p1*m1 + (p1+m1)*z1 + (z1-1)*z1//2
        ans2 += ans
        now = 0
        #print(p)
        now1 = p1-1
        now2 = 0
        now3 = p1-1
        if p1 > 1:
            while now < now1:
                if p[now]*p[now1] > x:
                    now1 -= 1
                else:
                    ans3 = max(ans3,p[now]*p[now1])
                    ans += now1-now
                    now += 1
            while now2 < now3:
                if p[now2]*p[now3] >= x:
                    now3 -= 1
                else:
                    ans2 += now3 - now2
                    now2 += 1
        if m1 > 1:
            now = 0
            now1 = m1-1
            now2 = 0
            now3 = m1-1
            while now < now1:
                if m[now]*m[now1] > x:
                    now1 -= 1
                else:
                    ans3 = max(ans3,m[now]*m[now1])
                    ans += now1 - now
                    now += 1
            while now2 < now3:
                if m[now2]*m[now3] >= x:
                    now3 -= 1
                else:
                    ans2 += now3 - now2
                    now2 += 1
    elif x == 0:
        p.sort()
        m.sort()
        if p1 > 0 and m1 > 0:
            ans3 = p[0]*m[m1-1]
        if z1 > 0:
            ans3 = 0
        ans += p1*m1+1
        ans2 += (p1+m1)*z1 + ans - 1
        if z1 > 1:
            ans2 += z1*(z1-1)//2
    else:
        p.sort(reverse=True)
        m.sort()
        now = 0
        now1 = m1-1
        now2 = 0
        now3 = m1-1
        if p1 > 0 and m1 > 0:
            while now < p1 and now1 >= 0:
                if p[now]*m[now1] > x:
                    now1 -= 1
                else:
                    ans3 = max(ans3,p[now]*m[now1])
                    ans += now1+1
                    now += 1
            while now2 < p1 and now3 >= 0:
                if p[now2]*m[now3] >= x:
                    now3 -= 1
                else:
                    ans2 += now3+1
                    now2 += 1
    return [ans,ans2,ans3]
while nb1 + 1 < nb2:
    #print(nb1,nb2)
    x = (nb1+nb2)//2
    #print(sytr(x,p,z,m,p1,z1,m1))
    if max(sytr(x,p,z,m,p1,z1,m1)[0],sytr(x,p,z,m,p1,z1,m1)[1]) < k:
        nb1 = x
    elif min(sytr(x,p,z,m,p1,z1,m1)[0],sytr(x,p,z,m,p1,z1,m1)[1]) > k:
        nb2 = x
    else:
        if abs(sytr(x,p,z,m,p1,z1,m1)[1]-sytr(x,p,z,m,p1,z1,m1)[0]) == 0:
            x = sytr(x,p,z,m,p1,z1,m1)[2]
            break
        else:
            break
print(x)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
p = []
z = []
m = []
for i in range(n):
    if a[i] > 0:
        p.append(a[i])
    elif a[i] == 0:
        z.append(a[i])
    else:
        m.append(a[i])
nb1 = 10**18+1
nb2 = -10**18-1
p1 = len(p)
z1 = len(z)
m1 = len(m)
#print(p,m)
#print(p1,z1,m1)
def sytr(x,p,z,m,p1,z1,m1):
    ans = 0
    ans2 = 0
    ans3 = -10**18
    if x > 0:
        m.sort(reverse=True)
        p.sort()
        ans += p1*m1 + (p1+m1)*z1 + (z1-1)*z1//2
        ans2 += ans
        now = 0
        #print(p)
        now1 = p1-1
        now2 = 0
        now3 = p1-1
        if p1 > 1:
            while now < now1:
                if p[now]*p[now1] > x:
                    now1 -= 1
                else:
                    ans3 = max(ans3,p[now]*p[now1])
                    ans += now1-now
                    now += 1
            while now2 < now3:
                if p[now2]*p[now3] >= x:
                    now3 -= 1
                else:
                    ans2 += now3 - now2
                    now2 += 1
        if m1 > 1:
            now = 0
            now1 = m1-1
            now2 = 0
            now3 = m1-1
            while now < now1:
                if m[now]*m[now1] > x:
                    now1 -= 1
                else:
                    ans3 = max(ans3,m[now]*m[now1])
                    ans += now1 - now
                    now += 1
            while now2 < now3:
                if m[now2]*m[now3] >= x:
                    now3 -= 1
                else:
                    ans2 += now3 - now2
                    now2 += 1
    elif x == 0:
        p.sort()
        m.sort()
        if p1 > 0 and m1 > 0:
            ans3 = p[0]*m[m1-1]
        if z1 > 0:
            ans3 = 0
        ans += p1*m1
        ans2 += (p1+m1)*z1 + ans
    else:
        p.sort(reverse=True)
        m.sort()
        now = 0
        now1 = m1-1
        now2 = 0
        now3 = m1-1
        if p1 > 0 and m1 > 0:
            while now < p1 and now1 >= 0:
                if p[now]*m[now1] > x:
                    now1 -= 1
                else:
                    ans3 = max(ans3,p[now]*m[now1])
                    ans += now1+1
                    now += 1
            while now2 < p1 and now3 >= 0:
                if p[now2]*m[now3] >= x:
                    now3 -= 1
                else:
                    ans2 += now3+1
                    now2 += 1
    return [ans,ans2,ans3]
while nb1 + 1 < nb2:
    #print(nb1,nb2)
    x = (nb1+nb2)//2
    #print(sytr(x,p,z,m,p1,z1,m1))
    if max(sytr(x,p,z,m,p1,z1,m1)[0],sytr(x,p,z,m,p1,z1,m1)[1]) < k:
        nb1 = x
    elif min(sytr(x,p,z,m,p1,z1,m1)[0],sytr(x,p,z,m,p1,z1,m1)[1]) > k:
        nb2 = x
    else:
        if abs(sytr(x,p,z,m,p1,z1,m1)[1]-sytr(x,p,z,m,p1,z1,m1)[0]) == 0:
            x = sytr(x,p,z,m,p1,z1,m1)[2]
            break
        else:
            break
print(x)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
p = []
z = []
m = []
for i in range(n):
    if a[i] > 0:
        p.append(a[i])
    elif a[i] == 0:
        z.append(a[i])
    else:
        m.append(a[i])
nb1 = a[0]*a[n-1]
nb2 = max(a[0]*a[1],a[n-1]*a[n-2])
p1 = len(p)
z1 = len(z)
m1 = len(m)
#print(p,m)
#print(p1,z1,m1)
def sytr(x,p,z,m,p1,z1,m1):
    ans = 0
    ans2 = 0
    ans3 = -10**18
    if x > 0:
        m.sort(reverse=True)
        p.sort()
        ans += p1*m1 + (p1+m1)*z1 + (z1-1)*z1//2
        ans2 += ans
        now = 0
        #print(p)
        now1 = p1-1
        now2 = 0
        now3 = p1-1
        if p1 > 1:
            while now < now1:
                if p[now]*p[now1] > x:
                    now1 -= 1
                else:
                    ans3 = max(ans3,p[now]*p[now1])
                    ans += now1-now
                    now += 1
            while now2 < now3:
                if p[now2]*p[now3] >= x:
                    now3 -= 1
                else:
                    ans2 += now3 - now2
                    now2 += 1
        if m1 > 1:
            now = 0
            now1 = m1-1
            now2 = 0
            now3 = m1-1
            while now < now1:
                if m[now]*m[now1] > x:
                    now1 -= 1
                else:
                    ans3 = max(ans3,m[now]*m[now1])
                    ans += now1 - now
                    now += 1
            while now2 < now3:
                if m[now2]*m[now3] >= x:
                    now3 -= 1
                else:
                    ans2 += now3 - now2
                    now2 += 1
    elif x == 0:
        p.sort()
        m.sort()
        if p1 > 0 and m1 > 0:
            ans3 = p[0]*m[m1-1]
        if z1 > 0:
            ans3 = 0
        ans += p1*m1
        ans2 += (p1+m1)*z1 + ans
    else:
        p.sort(reverse=True)
        m.sort()
        now = 0
        now1 = m1-1
        now2 = 0
        now3 = m1-1
        if p1 > 0 and m1 > 0:
            while now < p1 and now1 >= 0:
                if p[now]*m[now1] > x:
                    now1 -= 1
                else:
                    ans3 = max(ans3,p[now]*m[now1])
                    ans += now1+1
                    now += 1
            while now2 < p1 and now3 >= 0:
                if p[now2]*m[now3] >= x:
                    now3 -= 1
                else:
                    ans2 += now3+1
                    now2 += 1
    return [ans,ans2,ans3]
while nb1 + 1 < nb2:
    #print(nb1,nb2)
    x = (nb1+nb2)//2
    #print(sytr(x,p,z,m,p1,z1,m1))
    if max(sytr(x,p,z,m,p1,z1,m1)[0],sytr(x,p,z,m,p1,z1,m1)[1]) < k:
        nb1 = x
    elif min(sytr(x,p,z,m,p1,z1,m1)[0],sytr(x,p,z,m,p1,z1,m1)[1]) > k:
        nb2 = x
    else:
        if abs(sytr(x,p,z,m,p1,z1,m1)[1]-sytr(x,p,z,m,p1,z1,m1)[0]) == 0:
            x = sytr(x,p,z,m,p1,z1,m1)[2]
            break
        else:
            break
print(x)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
p = []
z = []
m = []
for i in range(n):
    if a[i] > 0:
        p.append(a[i])
    elif a[i] == 0:
        z.append(a[i])
    else:
        m.append(a[i])
nb1 = a[0]*a[n-1]
nb2 = max(a[0]*a[1],a[n-1]*a[n-2])
p1 = len(p)
z1 = len(z)
m1 = len(m)
#print(p,m)
#print(p1,z1,m1)
def sytr(x,p,z,m,p1,z1,m1):
    ans = 0
    ans2 = 0
    ans3 = -10**18
    if x > 0:
        m.sort(reverse=True)
        p.sort()
        ans += p1*m1 + (p1+m1)*z1 + (z1-1)*z1//2
        ans2 += ans
        now = 0
        #print(p)
        now1 = p1-1
        now2 = 0
        now3 = p1-1
        if p1 > 1:
            while now < now1:
                if p[now]*p[now1] > x:
                    now1 -= 1
                else:
                    ans3 = max(ans3,p[now]*p[now1])
                    ans += now1-now
                    now += 1
            while now2 < now3:
                if p[now2]*p[now3] >= x:
                    now3 -= 1
                else:
                    ans2 += now3 - now2
                    now2 += 1
        if m1 > 1:
            now = 0
            now1 = m1-1
            now2 = 0
            now3 = m1-1
            while now < now1:
                if m[now]*m[now1] > x:
                    now1 -= 1
                else:
                    ans3 = max(ans3,m[now]*m[now1])
                    ans += now1 - now
                    now += 1
            while now2 < now3:
                if m[now2]*m[now3] >= x:
                    now3 -= 1
                else:
                    ans2 += now3 - now2
                    now2 += 1
    elif x == 0:
        p.sort()
        m.sort()
        if p1 > 0 and m1 > 0:
            ans3 = p[0]*m[m1-1]
        if z1 > 0:
            ans3 = 0
        ans += p1*m1
        ans2 += (p1+m1)*z1 + ans
    else:
        p.sort(reverse=True)
        m.sort()
        now = 0
        now1 = m1-1
        now2 = 0
        now3 = m1-1
        if p1 > 0 and m1 > 0:
            while now < p1 and now1 >= 0:
                if p[now]*m[now1] > x:
                    now1 -= 1
                else:
                    ans3 = max(ans3,p[now]*m[now1])
                    ans += now1+1
                    now += 1
            while now2 < p1 and now3 >= 0:
                if p[now2]*m[now3] >= x:
                    now3 -= 1
                else:
                    ans2 += now3+1
                    now2 += 1
    return [ans,ans2,ans3]
while nb1 + 1 < nb2:
    #print(nb1,nb2)
    x = (nb1+nb2)//2
    #print(sytr(x,p,z,m,p1,z1,m1))
    if max(sytr(x,p,z,m,p1,z1,m1)[0],sytr(x,p,z,m,p1,z1,m1)[1]) < k:
        nb1 = x
    elif min(sytr(x,p,z,m,p1,z1,m1)[0],sytr(x,p,z,m,p1,z1,m1)[1]) > k:
        nb2 = x
    else:
        if abs(sytr(x,p,z,m,p1,z1,m1)[1]-sytr(x,p,z,m,p1,z1,m1)[0]) == 0:
            x = sytr(x,p,z,m,p1,z1,m1)[2]
            break
        else:
            break
print(x)