Problem:
 Score : 600 points    Problem Statement There is a function f(x) , which is initially a constant function f(x) = 0 .  We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows:   An update query 1 a b : Given two integers a and b , let g(x) = f(x) + |x - a| + b and replace f(x) with g(x) .  An evaluation query 2 : Print x that minimizes f(x) , and the minimum value of f(x) . If there are multiple such values of x , choose the minimum such value.   We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points.      Constraints  All values in input are integers.  1 \leq Q \leq 2 \times 10^5  -10^9 \leq a, b \leq 10^9  The first query is an update query.         Input Input is given from Standard Input in the following format:  Q  Query_1  :  Query_Q   See Sample Input 1 for an example.      Output For each evaluation query, print a line containing the response, in the order in which the queries are given.  The response to each evaluation query should be the minimum value of x that minimizes f(x) , and the minimum value of f(x) , in this order, with space in between.        Sample Input 1 4
1 4 2
2
1 1 -8
2      Sample Output 1 4 2
1 -3  In the first evaluation query, f(x) = |x - 4| + 2 , which attains the minimum value of 2 at x = 4 .  In the second evaluation query, f(x) = |x - 1| + |x - 4| - 6 , which attains the minimum value of -3 when 1 \leq x \leq 4 . Among the multiple values of x that minimize f(x) , we ask you to print the minimum, that is, 1 .       Sample Input 2 4
1 -1000000000 1000000000
1 -1000000000 1000000000
1 -1000000000 1000000000
2      Sample Output 2 -1000000000 3000000000

Solution (Python):
import heapq

def calc(keisuu_lower_max, keisuu_lower_sum,  keisuu_lower_count,
         keisuu_upper_sum, keisuu_upper_count, teisuu):
    print(keisuu_lower_max,
          - keisuu_lower_sum + keisuu_lower_count * keisuu_lower_max
          + keisuu_upper_sum - keisuu_upper_count * keisuu_lower_max + teisuu)


def lowerpush(keisuu_lower, a, keisuu_lower_max,  keisuu_lower_sum, keisuu_lower_count):
    heapq.heappush(keisuu_lower, -a)
    keisuu_lower_count += 1
    keisuu_lower_max = -keisuu_lower[0]
    keisuu_lower_sum += a
    return keisuu_lower, a, keisuu_lower_max,  keisuu_lower_sum, keisuu_lower_count


def upperpush(keisuu_upper, a, keisuu_upper_min, keisuu_upper_sum, keisuu_upper_count):
    heapq.heappush(keisuu_upper, a)
    keisuu_upper_count += 1
    keisuu_upper_min = keisuu_upper[0]
    keisuu_upper_sum += a
    return keisuu_upper, a, keisuu_upper_min,  keisuu_upper_sum, keisuu_upper_count


def from_lower_to_upper(keisuu_lower, keisuu_lower_max, keisuu_lower_sum, keisuu_lower_count,
                        keisuu_upper, keisuu_upper_min, keisuu_upper_sum, keisuu_upper_count):

    e = -heapq.heappop(keisuu_lower)
    heapq.heappush(keisuu_upper, e)
    keisuu_lower_sum -= e
    keisuu_upper_sum += e
    keisuu_lower_max = -keisuu_lower[0]
    keisuu_upper_min = e
    keisuu_lower_count -= 1
    keisuu_upper_count += 1

    return keisuu_lower, keisuu_lower_max, keisuu_lower_sum, keisuu_lower_count, keisuu_upper, keisuu_upper_min, keisuu_upper_sum, keisuu_upper_count


def from_upper_to_lower(keisuu_lower, keisuu_lower_max, keisuu_lower_sum, keisuu_lower_count,
                        keisuu_upper, keisuu_upper_min, keisuu_upper_sum, keisuu_upper_count):

    d = heapq.heappop(keisuu_upper)
    heapq.heappush(keisuu_lower, -d)
    keisuu_lower_sum += d
    keisuu_upper_sum -= d
    keisuu_lower_max = d
    keisuu_upper_min = keisuu_upper[0]
    keisuu_lower_count += 1
    keisuu_upper_count -= 1

    return keisuu_lower, keisuu_lower_max, keisuu_lower_sum, keisuu_lower_count, keisuu_upper, keisuu_upper_min, keisuu_upper_sum, keisuu_upper_count


def main():
    q = int(input())
    keisuu_lower = []
    keisuu_upper = []
    keisuu_lower_max = 0
    keisuu_upper_min = float("inf")
    keisuu_lower_count = 0
    keisuu_upper_count = 0
    keisuu_lower_sum = 0
    keisuu_upper_sum = 0
    teisuu = 0

    for i in range(q):
        query = input()
        if query == "2":
            calc(keisuu_lower_max, keisuu_lower_sum, keisuu_lower_count,
                 keisuu_upper_sum, keisuu_upper_count, teisuu)

        else:
            _, a, b = [int(i) for i in query.split()]
            teisuu += b

            if a < keisuu_lower_max:
                keisuu_lower, a, keisuu_lower_max, keisuu_lower_sum, keisuu_lower_count \
                    = lowerpush(keisuu_lower, a, keisuu_lower_max, keisuu_lower_sum, keisuu_lower_count)

            elif a > keisuu_upper_min:
                keisuu_upper, a, keisuu_upper_min, keisuu_upper_sum, keisuu_upper_count \
                    = upperpush(keisuu_upper, a, keisuu_upper_min, keisuu_upper_sum, keisuu_upper_count)

            elif keisuu_lower_count == keisuu_upper_count:
                keisuu_lower, a, keisuu_lower_max, keisuu_lower_sum, keisuu_lower_count \
                    = lowerpush(keisuu_lower, a, keisuu_lower_max, keisuu_lower_sum, keisuu_lower_count)

            else:
                keisuu_upper, a, keisuu_upper_min, keisuu_upper_sum, keisuu_upper_count \
                    = upperpush(keisuu_upper, a, keisuu_upper_min, keisuu_upper_sum, keisuu_upper_count)

            if keisuu_lower_count > keisuu_upper_count + 1:
                keisuu_lower, keisuu_lower_max, keisuu_lower_sum, keisuu_lower_count, keisuu_upper, keisuu_upper_min, keisuu_upper_sum, keisuu_upper_count \
                    = from_lower_to_upper(keisuu_lower, keisuu_lower_max, keisuu_lower_sum, keisuu_lower_count,
                                          keisuu_upper, keisuu_upper_min, keisuu_upper_sum, keisuu_upper_count)

            elif keisuu_lower_count < keisuu_upper_count:
                keisuu_lower, keisuu_lower_max, keisuu_lower_sum, keisuu_lower_count, keisuu_upper, keisuu_upper_min, keisuu_upper_sum, keisuu_upper_count \
                    = from_upper_to_lower(keisuu_lower, keisuu_lower_max, keisuu_lower_sum, keisuu_lower_count,
                                          keisuu_upper, keisuu_upper_min, keisuu_upper_sum, keisuu_upper_count)


main()

Solution (Python):
import heapq

Q = int(input())
ans = 0

L = []
R = []

for _ in range(Q):
    q = list(map(int, input().split()))
    if q[0] == 1:
        l = heapq.heappushpop(L, -q[1])
        r = heapq.heappushpop(R, q[1])
        heapq.heappush(L, -r)
        heapq.heappush(R, -l)
        ans += abs(l + r) + q[2]
    else:
        print(-L[0], ans)

Solution (Python):
import heapq
Q = int(input())
INF = 2 * (10 ** 9)
a1, a2 = [INF], [INF]
asum1 = 0
asum2 = 0
bsum = 0
for i in range(Q):
    query = list(map(int, input().split()))
    if query[0] == 3:
        break
    if query[0] == 1:
        a, b = query[1:]
        bsum += b
        if len(a1) == len(a2):
            if -a1[0] > a:
                asum1 += a
                heapq.heappush(a1, -a)
            else:
                asum2 += a
                heapq.heappush(a2, a)
                asum1 += a2[0]
                asum2 -= a2[0]
                heapq.heappush(a1, -heapq.heappop(a2))
        else:
            if a2[0] < a:
                asum2 += a
                heapq.heappush(a2, a)
            else:
                asum1 += a
                heapq.heappush(a1, -a)
                asum2 += -a1[0]
                asum1 -= -a1[0]
                heapq.heappush(a2, -heapq.heappop(a1))
    else:
        m = -a1[0]
        print(m, m * (len(a1) - 1) -
              asum1 + asum2 - m * (len(a2) - 1) + bsum)

Solution (Python):


from sys import stdout
from bisect import bisect_left as binl
from copy import copy, deepcopy
from collections import defaultdict
import heapq


mod = 1


def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


def intina():
    return [int(i) for i in input().split()]


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * y) % mod


def lcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x


def combination(x, y):
    assert(x >= y)

    if y > x // 2:
        y = x - y

    ret = 1

    for i in range(0, y):
        j = x - i
        i = i + 1

        ret = ret * j
        ret = ret // i

    return ret


def get_divisors(x):
    retlist = []
    for i in range(1, int(x**0.5) + 3):
        if x % i == 0:
            retlist.append(i)
            retlist.append(x // i)
    return retlist


def get_factors(x):
    retlist = []
    for i in range(2, int(x**0.5) + 3):
        while x % i == 0:
            retlist.append(i)
            x = x // i
    retlist.append(x)
    return retlist


def make_linklist(xylist):
    linklist = {}
    for a, b in xylist:
        linklist.setdefault(a, [])
        linklist.setdefault(b, [])
        linklist[a].append(b)
        linklist[b].append(a)
    return linklist


def calc_longest_distance(linklist, v=1):
    distance_list = {}
    distance_count = 0
    distance = 0
    vlist_previous = []
    vlist = [v]
    nodecount = len(linklist)

    while distance_count < nodecount:
        vlist_next = []
        for v in vlist:
            distance_list[v] = distance
            distance_count += 1
            vlist_next.extend(linklist[v])
        distance += 1
        vlist_to_del = vlist_previous
        vlist_previous = vlist
        vlist = list(set(vlist_next) - set(vlist_to_del))

    max_distance = -1
    max_v = None
    for v, distance in distance_list.items():
        if distance > max_distance:
            max_distance = distance
            max_v = v

    return (max_distance, max_v)


def calc_tree_diameter(linklist, v=1):
    _, u = calc_longest_distance(linklist, v)
    distance, _ = calc_longest_distance(linklist, u)
    return distance


class UnionFind:

    def __init__(self, n):
        self.parent = [i for i in range(n)]

    def root(self, i):
        if self.parent[i] == i:
            return i
        self.parent[i] = self.root(self.parent[i])
        return self.parent[i]

    def unite(self, i, j):
        rooti = self.root(i)
        rootj = self.root(j)
        if rooti == rootj:
            return
        if rooti < rootj:
            self.parent[rootj] = rooti
        else:
            self.parent[rooti] = rootj

    def same(self, i, j):
        return self.root(i) == self.root(j)





def main():
    qnum = intin()
    qlist = [input() for _ in range(qnum)]

    lalist = []
    ralist = []
    acount = 0
    lsum = 0
    rsum = 0
    bsum = 0

    for q in qlist:

        if q[0] == '1':
            q = q.split(' ')
            a = int(q[1])
            b = int(q[2])

            bsum += b

            if acount:
                _, prev_med = lalist[0]
                if acount & 1:
                    acount += 1
                    if a <= prev_med:
                        heapq.heappop(lalist)
                        heapq.heappush(lalist, (-a, a))
                        heapq.heappush(ralist, (prev_med, prev_med))
                        _, med = lalist[0]
                        lsum -= (abs(prev_med - med) * ((acount - 1) // 2))
                        lsum += abs(med - a)
                        rsum += (abs(prev_med - med) * (acount // 2))
                    else:
                        heapq.heappush(ralist, (a, a))
                        _, med = lalist[0]
                        rsum += abs(med - a)
                else:
                    acount += 1
                    if a < prev_med:
                        heapq.heappush(lalist, (-a, a))
                        _, med = lalist[0]
                        lsum += abs(med - a)
                    else:
                        _, tmp = heapq.heappop(ralist)
                        l = min(a, tmp)
                        r = max(a, tmp)
                        heapq.heappush(ralist, (r, r))
                        heapq.heappush(lalist, (-l, l))
                        _, med = lalist[0]
                        lsum += (abs(med - prev_med) * (acount // 2))
                        rsum -= (abs(med - prev_med) * (acount // 2))
                        rsum += abs(a - med)

            else:
                lalist.append((-a, a))
                acount += 1

        if q[0] == '2':
            _, med = lalist[0]
            print('%d %d' % (med, lsum + rsum + bsum))


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
Q = int(input())
Y = []
D = []
S = [0] * Q
s = 0
for i in range(Q):
    a = list(map(int, input().split()))
    Y.append(a)
    if a[0] == 1:
        D.append(a[1])
        s += a[2]
    S[i] = s

D = sorted(list(set(D)))
INV = {}
for i in range(len(D)):
    INV[D[i]] = i

N = 17
X = [0] * (2**(N+1)-1)
C = [0] * (2**(N+1)-1)

def add(j, x):
    i = 2**N + j - 1
    while i >= 0:
        X[i] += x
        C[i] += 1
        i = (i-1) // 2

def rangesum(a, b):
    l = a + (1<<N)
    r = b + (1<<N)
    s = 0
    while l < r:
        if l%2:
            s += X[l-1]
            l += 1
        if r%2:
            r -= 1
            s += X[r-1]
        l >>= 1
        r >>= 1
    return s
def rangecnt(a, b):
    l = a + (1<<N)
    r = b + (1<<N)
    s = 0
    while l < r:
        if l%2:
            s += C[l-1]
            l += 1
        if r%2:
            r -= 1
            s += C[r-1]
        l >>= 1
        r >>= 1
    return s

c = 0
su = 0
CC = [0] * (2**N)
for i in range(Q):
    y = Y[i]
    if y[0] == 1:
        add(INV[y[1]], y[1])
        CC[INV[y[1]]] += 1
        c += 1
        su += y[1]
    else:
        l, r = 0, 2**N
        while True:
            m = (l+r)//2
            rc = rangecnt(0, m)
            if rc >= (c+1)//2:
                r = m
            elif rc + CC[m] <= (c-1)//2:
                l = m
            else:
                break
        rs = rangesum(0, m)
        print(D[m], D[m]*rc-rs+(su-rs)-(c-rc)*D[m]+S[i])

Solution (Python):
import sys
input = sys.stdin.readline
Q = int(input())
Y = []
D = []
S = [0] * Q
s = 0
for i in range(Q):
    a = list(map(int, input().split()))
    Y.append(a)
    if a[0] == 1:
        D.append(a[1])
        s += a[2]
    S[i] = s

D = sorted(list(set(D)))
INV = {}
for i in range(len(D)):
    INV[D[i]] = i

N = 17
X = [0] * (2**(N+1)-1)
C = [0] * (2**(N+1)-1)

def add(j, x):
    i = 2**N + j - 1
    while i >= 0:
        X[i] += x
        C[i] += 1
        i = (i-1) // 2

def rangeof(i):
    s = (len(bin(i+1))-3)
    l = ((i+1) - (1<<s)) * (1<<N-s)
    r = l + (1<<N-s)
    return (l, r)

def rangesum(a, b):
    l = a + (1<<N)
    r = b + (1<<N)
    s = 0
    while l < r:
        if l%2:
            s += X[l-1]
            l += 1
        if r%2:
            r -= 1
            s += X[r-1]
        l >>= 1
        r >>= 1
    return s
def rangecnt(a, b):
    l = a + (1<<N)
    r = b + (1<<N)
    s = 0
    while l < r:
        if l%2:
            s += C[l-1]
            l += 1
        if r%2:
            r -= 1
            s += C[r-1]
        l >>= 1
        r >>= 1
    return s

c = 0
su = 0
CC = [0] * (2**N)
for i in range(Q):
    y = Y[i]
    if y[0] == 1:
        add(INV[y[1]], y[1])
        CC[INV[y[1]]] += 1
        c += 1
        su += y[1]
    else:
        l, r = 0, 2**N
        while True:
            m = (l+r)//2
            rc = rangecnt(0, m)
            if rc >= (c+1)//2:
                r = m
            elif rc + CC[m] <= (c-1)//2:
                l = m
            else:
                break
        rs = rangesum(0, m)
        print(D[m], D[m]*rc-rs+(su-rs)-(c-rc)*D[m]+S[i])

Solution (Python):
Q = int(input())
INF = 10 ** 9

queries = []
for _ in range(Q):
    queries.append(input())

from bisect import insort_left
from heapq import heappush, heappop

absum, bsum, c = 0, 0, 0

lefts, rights = [INF], [INF]
for query in queries:
    if query[0] == '2':
        
        
        
        
        print(-lefts[0], absum + bsum)
    else:
        _, a, b = map(int, query.split())
        if c % 2 == 0:
            if -lefts[0] <= a <= rights[0]:
                heappush(lefts, -a)
                heappush(rights, a)
                absum += 0
            elif a < -lefts[0]:
                heappush(lefts, -a)
                heappush(rights, -lefts[0])
                absum += -lefts[0] - a
            else:
                heappush(rights, a)
                heappush(lefts, -rights[0])
                absum += a - rights[0]
        else:
            if a < -lefts[0]:
                absum += -lefts[0] - a
                heappop(lefts)
                heappush(lefts, -a)
            else:
                absum += a - rights[0]
                heappop(rights)
                heappush(rights, a)
        
        bsum += b
        c += 1

Solution (Python):
from heapq import *
Q = int(input())
L, R = [], []
ans = 0
for _ in range(Q):
    a = [int(a) for a in input().split()]
    if a[0] == 1:
        t1 = heappushpop(L, -a[1])
        t2 = heappushpop(R, a[1])
        heappush(L, -t2)
        heappush(R, -t1)
        ans += a[2] + abs(t2+t1)
    else:
        print(-L[0], ans)

Solution (Python):
import heapq

Q = int(input())
b_sum = 0
a_list = []
left = []
right = []
res = 0

for i in range(Q):
    query = list(map(int, input().split()))
    if query[0] == 1:
        b_sum += query[2]

        if not left:
            a = query[1]
            heapq.heappush(left, -a)
        else:
            res += abs(query[1] + left[0])
            if query[1] <= -left[0]:
                heapq.heappush(left, -query[1])
            else:
                heapq.heappush(right, query[1])

        if len(left) < len(right):
            x = heapq.heappop(right)
            y = -left[0]
            res += y - x
            heapq.heappush(left, -x)
        elif len(left) > len(right) + 1:
            x = - heapq.heappop(left)
            if len(right) != 0:
                y = right[0]
            else:
                y = 0
            heapq.heappush(right, x)
    else:
        print(-left[0], res + b_sum)

Solution (Python):
import heapq
Q = int(input())

lefts, rights = [], []  
diff = 0    
bsum = 0    

for i in range(Q):
    query = list(map(int, input().split()))
    if query[0] == 1:   
        a = query[1]
        bsum += query[2]
        if len(lefts) == 0:
            heapq.heappush(lefts, -a)   
        else:
            if a <= -lefts[0]:
                heapq.heappush(lefts, -a)   
                diff -= a + lefts[0]        
            else:
                heapq.heappush(rights, a)   
                diff += a + lefts[0]        
        
        if len(lefts) < len(rights):    
            med = heapq.heappop(rights) 
            diff -= med + lefts[0]      
            heapq.heappush(lefts, -med) 
        elif len(lefts) > len(rights) + 1:  
            med = -heapq.heappop(lefts)      
            
            heapq.heappush(rights, med)     
    else:   
        x = -lefts[0]       
        fx = diff + bsum    
        print(x, fx)

Solution (Python):
import heapq

inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))

DBG = True  and False




q = inn()
x = inl()
root = x[1]
b = x[2]
tl = []
tr = []
nl = nr = suml = sumr = 0

for i in range(1,q):
    
    x = inl()
    if len(x)==1:
        z = sumr - suml - (root if nr>nl else 0) + b
        print("{} {}".format(root,z))
    else:
        a = x[1]
        b += x[2]
        if nl == nr:
            aa = -heapq.heappushpop(tl,-a)
            suml += a - aa
            if aa<root:
                aa,root = root,aa
            heapq.heappush(tr,aa)
            sumr += aa
            nr += 1
        else:
            aa = heapq.heappushpop(tr,a)
            sumr += a - aa
            if aa>root:
                aa,root = root,aa
            heapq.heappush(tl,-aa)
            suml += aa
            nl += 1

Solution (Python):
from heapq import *
Q = int(input())
L, R = [], []
ans = 0
for _ in range(Q):
    a = [int(a) for a in input().split()]
    if a[0] == 1:
        ans += a[2]
        if len(L) == 0 or a[1] <= -L[0]:
            heappush(L, -a[1])
            ans += -L[0] - a[1]
        else:
            heappush(R, a[1])
            ans += a[1] + L[0]
        if len(R) > len(L):
            ans -= R[0] + L[0]
            heappush(L, -heappop(R))
        elif len(L) - len(R) >= 2:
            heappush(R, -heappop(L))
    else:
        print(-L[0], ans)

Solution (Python):
from heapq import heappop, heappush
Q = int(input())
n, a, b = map(int, input().split())
mi = b
L = [-a]
R = [a]
num = 1
for i in range(Q - 1):
    q = list(map(int, input().split()))
    if len(q) == 3:
        if num % 2 != 0:
            l = -heappop(L)
            r = heappop(R)
            l1 = min(l, q[1])
            r1 = max(r, q[1])
            heappush(L, -l1)
            heappush(R, r1)
            mi += abs(l - q[1])
        else:
            if q[1] < -L[0]:
                heappush(L, -q[1])
                heappush(R, -L[0])
                mi -= q[1] + L[0]
            elif q[1] > R[0]:
                heappush(L, -R[0])
                heappush(R, q[1])
                mi += q[1] - R[0]
            else:
                heappush(L, -q[1])
                heappush(R, q[1])

        mi += q[2]
        num += 1
    else:
        print(-L[0], mi)

Solution (Python):
import sys
input=sys.stdin.readline
from heapq import heappush,heappop
q=int(input())
L,R=[],[]
s,c=0,0
for _ in range(q):
    query=tuple(map(int,input().split()))
    if query[0]==1:
        _,a,b=query
        c+=b
        if len(L)==0 or a<=-L[0]:
            heappush(L,-a)
            s+=-L[0]-a
        else:
            heappush(R,a)
            s+=a+L[0]
        if len(R)>len(L):
            s+=-R[0]-L[0]
            heappush(L,-heappop(R))
        if len(L)-len(R)==2:
            heappush(R,-heappop(L))
    else:
        print(-L[0],s+c)

Solution (Python):
import heapq
N = int(input())
st = input()
n, a, b = map(int,st.split())

minvalue = b
left = [-a]
right = [a]
ptnum = 1
for i in range(N-1):
  st = input()
  if st[0] == '1':
    n, a, b = map(int,st.split())
    if ptnum%2 == 1:
      l = - heapq.heappop(left)
      r = heapq.heappop(right)
      le = min(l,a)
      ri = max(a,r)
      heapq.heappush(left, -le)
      heapq.heappush(right, ri)
      minvalue += abs(l-a)
    else:
      if a < -left[0]:
        heapq.heappush(left, -a)
        heapq.heappush(right, -left[0])
        minvalue -= a+left[0]
      elif a > right[0]:
        heapq.heappush(left, -right[0])
        heapq.heappush(right, a)
        minvalue += a-right[0]
      else:
        heapq.heappush(left, -a)
        heapq.heappush(right, a)
        
    minvalue += b
    ptnum += 1

  else:
    print(- left[0], minvalue)

Solution (Python):
from heapq import*
L,R=[],[]
B=t=0
Q,*E=open(0)
for e in E:
	if' 'in e:_,a,b=map(int,e.split());t=1-t;a*=2*t-1;c=heappushpop([L,R][t],a);heappush([R,L][t],-c);B+=b+a-c-c
	else:w=-L[0];print(w,B+w*t)

Solution (Python):
from heapq import*
L,R=[],[]
B=0
Q,*E=open(0)
for e in E:
    if e[0]>'1':
        w=-L[0]
        print(w,B+w*(len(L)>len(R)))
    else:
        _,a,b=map(int,e.split())
        B+=b
        if len(L)>len(R):
            B-=a
            a=-heappushpop(L,-a)
            B+=a+a
            heappush(R,a)
        else:
            B+=a
            a=heappushpop(R,a)
            B-=a+a
            heappush(L,-a)

Solution (Python):
Q=int(input())
ans=0
from heapq import heappush,heappop
left=[]
right=[]
L=-1*float('inf')
R=float('inf')
for i in range(Q):
    q=input()
    if q=="2":
        print(" ".join([str(L),str(ans)]))
    else:
        s,a,b=map(int,q.split())
        ans+=b
        if L<=a<=R:
            heappush(left,-1*a)
            heappush(right,a)
            L=a
            R=a
        elif a<L:
            ans+=L-a
            x=heappop(left)
            heappush(right,-1*x)
            heappush(left,-1*a)
            heappush(left,-1*a)
        elif R<a:
            ans+=a-R
            x=heappop(right)
            heappush(left,-1*x)
            heappush(right,a)
            heappush(right,a)
        R=right[0]
        L=-1*left[0]

Solution (Python):
import sys
input = sys.stdin.readline
from heapq import *
Q = int(input())
L, R = [], []
ans = 0
for _ in range(Q):
    a = [int(a) for a in input().split()]
    if a[0] == 1:
        ans += a[2]
        if len(L) == 0 or a[1] <= -L[0]:
            heappush(L, -a[1])
            ans += -L[0] - a[1]
        else:
            heappush(R, a[1])
            ans += a[1] + L[0]
        if len(R) > len(L):
            ans -= R[0] + L[0]
            heappush(L, -heappop(R))
        elif len(L) - len(R) >= 2:
            heappush(R, -heappop(L))
    else:
        print(-L[0], ans)

Solution (Python):
from heapq import *
Q = int(input())
L, R = [], []
ans = 0
for _ in range(Q):
    a = [int(a) for a in input().split()]
    if a[0] == 1:
        ans += a[2]
        if len(L) == 0 or a[1] <= -L[0]:
            heappush(L, -a[1])
            ans += -L[0] - a[1]
        else:
            heappush(R, a[1])
            ans += a[1] + L[0]
        if len(R) > len(L):
            ans -= R[0] + L[0]
            heappush(L, -heappop(R))
        elif len(L) - len(R) >= 2:
            heappush(R, -heappop(L))
    else:
        print(-L[0], ans)

Solution (Python):
import sys
from heapq import heappush, heappushpop

Q = int(input())
high = []
low = []
sum_dh, sum_dl = 0, 0
sum_b = 0
buf = []

for qi, line in enumerate(sys.stdin):
    q = list(map(int, line.split()))
    if len(q) == 1:
        x = -low[0]
        ans_l = x * len(low) - sum_dl
        ans_h = sum_dh - x * len(high)
        buf.append('{} {}\n'.format(x, ans_l + ans_h + sum_b))
    else:
        _, a, b = q
        sum_b += b
        if len(low) == len(high):
            h = heappushpop(high, a)
            heappush(low, -h)
            sum_dh += a - h
            sum_dl += h
        else:
            l = -heappushpop(low, -a)
            heappush(high, l)
            sum_dl += a - l
            sum_dh += l

print(''.join(buf))

Solution (Python):
import sys
input = sys.stdin.readline
from heapq import *
Q = int(input())
L, R = [], []
ans = 0
for _ in range(Q):
    a = [int(a) for a in input().split()]
    if a[0] == 1:
        ans += a[2]
        if len(L) == 0 or a[1] <= -L[0]:
            heappush(L, -a[1])
            ans += -L[0] - a[1]
        else:
            heappush(R, a[1])
            ans += a[1] + L[0]
        if len(R) > len(L):
            ans -= R[0] + L[0]
            heappush(L, -heappop(R))
        elif len(L) - len(R) >= 2:
            heappush(R, -heappop(L))
    else:
        print(-L[0], ans)

Solution (Python):
import sys
from heapq import heappush, heappushpop

Q = int(input())
high = []
low = []
sum_dh, sum_dl = 0, 0
sum_b = 0
MIN_X = -(10 ** 9)
buf = []

for qi, line in enumerate(sys.stdin):
    q = list(map(int, line.split()))
    if len(q) == 1:
        x = -low[0]
        dx = x - MIN_X
        ans_l = dx * len(low) - sum_dl
        ans_h = sum_dh - dx * len(high)
        buf.append('{} {}\n'.format(x, ans_l + ans_h + sum_b))
    else:
        _, a, b = q
        da = a - MIN_X
        sum_b += b
        if len(low) == len(high):
            h = heappushpop(high, a)
            heappush(low, -h)
            dh = h - MIN_X
            sum_dh += da - dh
            sum_dl += dh
        else:
            l = -heappushpop(low, -a)
            heappush(high, l)
            dl = l - MIN_X
            sum_dl += da - dl
            sum_dh += dl

print(''.join(buf))

Solution (Python):
Q = int(input())

import heapq
import sys
input = sys.stdin.readline

INF = 10**18
L = []
R = []
heapq.heappush(L,INF)
heapq.heappush(R,INF)
L_size = 0
R_size = 0
sum_b = 0
sum_L = 0
sum_R = 0

def f1(a,b):
  global sum_b,L_size,R_size,sum_L,sum_R
  sum_b += b
  L_max = -heapq.heappop(L)
  R_min = heapq.heappop(R)
  sum_L -= L_max
  sum_R -= R_min
  X = sorted([L_max,a,R_min])
  if L_size == R_size:
    heapq.heappush(L,-X[0])
    heapq.heappush(L,-X[1])
    heapq.heappush(R,X[2])
    L_size += 1
    sum_L += X[0] + X[1]
    sum_R += X[2]
  else:
    heapq.heappush(L,-X[0])
    heapq.heappush(R,X[1])
    heapq.heappush(R,X[2])
    R_size += 1
    sum_L += X[0]
    sum_R += X[1] + X[2]
  return

def f2():
  min_value = sum_b
  min_x = -L[0]
  if L_size == R_size:
    min_value += sum_R - sum_L
  else:
    min_value += sum_R - sum_L + min_x
  print(min_x,min_value)

for _ in range(Q):
  q = [int(x) for x in input().split()]
  if q[0]&1:
    f1(q[1],q[2])
  else:
    f2()

Solution (Python):
from heapq import heappush, heappop

num_q = int(input())
x = 0
f = 0
low = []
high = []
odd = False
num_a = 0
 
for _ in range(num_q):
  q = input()
  if q[0] == '2':
    print(x, f)
  else:
    _, n, m = map(int, q.split(" "))
    if odd:
      if n >= -low[0]:
        f += m + n + low[0]
        heappush(high, n)
      else:
        f += m - n - low[0]
        heappush(low, -n)
        heappush(high, -heappop(low))
    else:
      f += m
      if num_a == 0:
        heappush(low, -n)
      else:
        if n > -low[0]:
          if n > high[0]:
            f += n - high[0]
          heappush(high, n)
          heappush(low, -heappop(high))
        else:
          f += - n - low[0]
          heappush(low, -n)

    odd = ~odd
    x = -low[0]
    num_a += 1

Solution (Python):
Q = int(input())

import heapq
import sys
input = sys.stdin.readline

INF = 10**18
L = []
R = []
heapq.heappush(L,INF)
heapq.heappush(R,INF)
L_size = 0
R_size = 0
sum_b = 0
sum_L = 0
sum_R = 0

def f1(a,b):
  global sum_b,L_size,R_size,sum_L,sum_R
  sum_b += b
  L_max = -heapq.heappop(L)
  R_min = heapq.heappop(R)
  sum_L -= L_max
  sum_R -= R_min
  X = sorted([L_max,a,R_min])
  if L_size == R_size:
    heapq.heappush(L,-X[0])
    heapq.heappush(L,-X[1])
    heapq.heappush(R,X[2])
    L_size += 1
    sum_L += X[0] + X[1]
    sum_R += X[2]
  else:
    heapq.heappush(L,-X[0])
    heapq.heappush(R,X[1])
    heapq.heappush(R,X[2])
    R_size += 1
    sum_L += X[0]
    sum_R += X[1] + X[2]
  return

def f2():
  min_value = sum_b
  min_x = -L[0]
  if L_size == R_size:
    min_value += sum_R - sum_L
  else:
    min_value += sum_R - sum_L + min_x
  print(min_x,min_value)

QUERY = [[int(x) for x in input().split()] for _ in range(Q)]
for q in QUERY:
  if q[0]&1:
    f1(q[1],q[2])
  else:
    f2()

Solution (Python):
Q = int(input())

import heapq
import sys
input = sys.stdin.readline

INF = 10**18
L = []
R = []
heapq.heappush(L,INF)
heapq.heappush(R,INF)
L_size = 0
R_size = 0
sum_b = 0
sum_L = 0
sum_R = 0

def f1(a,b):
  global sum_b,L_size,R_size,sum_L,sum_R
  sum_b += b
  L_max = -heapq.heappop(L)
  R_min = heapq.heappop(R)
  sum_L -= L_max
  sum_R -= R_min
  X = sorted([L_max,a,R_min])
  if L_size == R_size:
    heapq.heappush(L,-X[0])
    heapq.heappush(L,-X[1])
    heapq.heappush(R,X[2])
    L_size += 1
    sum_L += X[0] + X[1]
    sum_R += X[2]
  else:
    heapq.heappush(L,-X[0])
    heapq.heappush(R,X[1])
    heapq.heappush(R,X[2])
    R_size += 1
    sum_L += X[0]
    sum_R += X[1] + X[2]
  return

def f2():
  min_value = sum_b
  min_x = -L[0]
  if L_size == R_size:
    min_value += sum_R - sum_L
  else:
    min_value += sum_R - sum_L + min_x
  print(min_x,min_value)

QUERY = [[int(x) for x in input().split()] for _ in range(Q)]
for q in QUERY:
  if q[0]&1:
    f1(q[1],q[2])
  else:
    f2()