Problem p03040 (python)

Fixed submission:
import sys
import io, os
#input = sys.stdin.buffer.readline
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

q = int(input())

import heapq
L = []
R = []
heapq.heapify(L)
heapq.heapify(R)

B = 0

for i in range(q):
    query = list(map(int, input().split()))
    if query[0] == 1:
        a, b = query[1], query[2]
        B += b
        heapq.heappush(L, -a)
        heapq.heappush(R, a)
        if -L[0] > R[0]:
            l = -heapq.heappop(L)
            r = heapq.heappop(R)
            B += l-r
            heapq.heappush(L, -r)
            heapq.heappush(R, l)
    else:
        print(-L[0], B)

Buggy submission (verdict: Runtime Error):
import sys
import io, os
#input = sys.stdin.buffer.readline
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

Q = int(input())
cnt = 0
m = []
M = []
import heapq
heapq.heapify(m)
heapq.heapify(M)
sum_m = 0
sum_M = 0
B = 0
cnt = 0
for i in range(Q):
    #print(m, M)
    q = str(input())
    if q != '2':
        _, a, b = map(int, q.split())
        B += b
        cnt += 1
        if len(m) != 0 and len(M) != 0:
            x1 = heapq.heappop(m)*(-1)
            x2 = heapq.heappop(M)
            sum_m -= x1
            sum_M -= x2
            if a <= x2:
                heapq.heappush(m, a*(-1))
                heapq.heappush(m, x1*(-1))
                sum_m += a+x1
                heapq.heappush(M, x2)
                sum_M += x2
            else:
                heapq.heappush(M, a)
                sum_M += a
                heapq.heappush(m, x1*(-1))
                heapq.heappush(M, x2*(-1))
                sum_m += x1+x2
        elif len(m) != 0 and len(M) == 0:
            x1 = heapq.heappop(m)*(-1)
            sum_m -= x1
            if a <= x1:
                heapq.heappush(m, a*(-1))
                sum_m += a
                heapq.heappush(M, x1)
                sum_M += x1
            else:
                heapq.heappush(M, a)
                sum_M += a
                heapq.heappush(m, x1*(-1))
                sum_m += x1
        else:
            heapq.heappush(m, a*(-1))
            sum_m += a
    else:
        if cnt == 0:
            print(0, 0)
        else:
            m_ = len(m)
            M_ = len(M)
            x = heapq.heappop(m)*(-1)
            min_ = sum_M - sum_m + m_*x - M_*x + B
            #print(sum_M, sum_m, B)
            print(x, min_)
            heapq.heappush(m, x*(-1))

Buggy submission (verdict: Runtime Error):
import sys
import io, os
input = sys.stdin.buffer.readline
#input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

q = int(input())

import heapq
h1 = []
h2 = []
heapq.heapify(h1)
heapq.heapify(h2)

B = 0
S1 = 0
S2 = 0

for i in range(q):
    query = list(map(int, input().split()))
    if query[0] == 1:
        a, b = query[1], query[2]
        B += b
        if len(h1) == 0:
            heapq.heappush(h1, a*(-1))
            S1 += a
        elif len(h2) == 0:
            x = heapq.heappop(h1)*(-1)
            S1 -= x
            if x <= a:
                heapq.heappush(h1, x*(-1))
                heapq.heappush(h2, a)
                S1 += x
                S2 += a
            else:
                heapq.heappush(h1, a*(-1))
                heapq.heappush(h2, x)
                S1 += a
                S2 += x
        else:
            x = heapq.heappop(h1)*(-1)
            y = heapq.heappop(h2)
            S1 -= x
            S2 -= y
            if a <= y:
                heapq.heappush(h1, x*(-1))
                heapq.heappush(h1, a*(-1))
                heapq.heappush(h2, y)
                S1 += x+a
                S2 += y
            else:
                heapq.heappush(h1, x*(-1))
                heapq.heappush(h1, y*(-1))
                heapq.haeppush(h2, a)
                S1 += x+y
                S2 += a
    else:
        t = h1[0]*(-1)
        ans = t*len(h1)-S1+S2-t*len(h2)+B
        print(t, ans)