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):
from heapq import heappush, heappop

def main():
    INF = 10 ** 18

    sum_B = 0
    L, R = [INF], [INF]
    sum_L = sum_R = 0
    L_size = R_size = 0

    A = map(int, open(0).read().split())

    Q = next(A)
    for _ in range(Q):
        q = next(A)
        if q == 1:
            a, b = next(A), next(A)

            sum_B += b

            max_L = -heappop(L)
            min_R = heappop(R)

            sum_L -= max_L
            sum_R -= min_R

            l, m, r = sorted((max_L, a, min_R))

            heappush(L, -l)
            heappush(R, r)
            sum_L += l
            sum_R += r

            if L_size == R_size:
                heappush(L, -m)
                L_size += 1
                sum_L += m
            else:
                heappush(R, m)
                R_size += 1
                sum_R += m

        else:
            median = -L[0]
            min_value = sum_B + sum_R - sum_L
            if L_size != R_size:
                min_value += median
            print(median, min_value)


if __name__ == '__main__':
    main()

Solution (Python):
from heapq import heappush, heappop

INF = 10 ** 18

sum_B = 0
L, R = [INF], [INF]
sum_L = sum_R = 0
L_size = R_size = 0

A = map(int, open(0).read().split())

Q = next(A)
for _ in range(Q):
    q = next(A)
    if q == 1:
        a, b = next(A), next(A)

        sum_B += b

        max_L = -heappop(L)
        min_R = heappop(R)

        sum_L -= max_L
        sum_R -= min_R

        l, m, r = sorted((max_L, a, min_R))

        heappush(L, -l)
        heappush(R, r)
        sum_L += l
        sum_R += r

        if L_size == R_size:
            heappush(L, -m)
            L_size += 1
            sum_L += m
        else:
            heappush(R, m)
            R_size += 1
            sum_R += m

    else:
        median = -L[0]
        min_value = sum_B + sum_R - sum_L
        if L_size != R_size:
            min_value += median
        print(median, min_value)

Solution (Python):
from heapq import heappush, heappop

INF = 10 ** 18

sum_B = 0
L, R = [INF], [INF]
sum_L = sum_R = 0
L_size = R_size = 0

A = map(int, open(0).read().split())

Q = next(A)
for _ in range(Q):
    q = next(A)
    if q == 1:
        a, b = next(A), next(A)

        sum_B += b

        max_L = -heappop(L)
        min_R = heappop(R)

        sum_L -= max_L
        sum_R -= min_R

        l, m, r = sorted((max_L, a, min_R))
        if L_size == R_size:
            heappush(L, -l)
            heappush(L, -m)
            heappush(R, r)
            L_size += 1
            sum_L += l + m
            sum_R += r
        else:
            heappush(L, -l)
            heappush(R, m)
            heappush(R, r)
            R_size += 1
            sum_L += l
            sum_R += m + r
    else:
        median = -L[0]
        min_value = sum_B + sum_R - sum_L
        if L_size != R_size:
            min_value += median
        print(median, min_value)

Solution (Python):

import sys
input = sys.stdin.readline
def bsearch(target, min_i, max_i, func):
    
    if func(max_i) <= target:
        return max_i
    if target < func(min_i):
        return None
    index = (max_i + min_i)//2
    while True:
        if func(index) <= target:
            if target < func(index+1):
                return index
            index, min_i = (index+1 + max_i)//2, index+1
            continue
        index, max_i = (index-1 + min_i)//2, index-1

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

q=int(input())
xs = []
ys = []
qs = dict()
c = 0
for _ in range(q):
    xy = list(map(int,input().split()))
    if len(xy) == 1:
        if c not in qs:
            qs[c] = 0
        qs[c] += 1
    else:
        c += 1
        xs.append(xy[1])
        ys.append(xy[2])

N=len(xs)
sxs=sorted(xs)
zxs = [0]*N
for i, x in enumerate(sorted(range(N), key=lambda x : xs[x])):
    zxs[x] = i

seg_sum_tree = Bit(N)
seg_count_tree = Bit(N)

sy = 0
for i in range(c+1):
    if i in qs:
        x1i=bsearch(i//2 + (-1 if i % 2 == 0 else 0) ,0 ,N ,lambda x : seg_count_tree.sum(x))

        if i % 2:
        
            r = sy - seg_sum_tree.sum(x1i) + \
                   seg_sum_tree.sum(N) - seg_sum_tree.sum(x1i+1)
        else:
            r = sy - seg_sum_tree.sum(x1i+1) + \
                   seg_sum_tree.sum(N) - seg_sum_tree.sum(x1i+1)
        for _ in range(qs[i]):
            print(sxs[x1i], r)
    if i == c:
        break
    x, y = xs[i], ys[i]
    xi = zxs[i]
    sy += y
    seg_sum_tree.add(xi+1, x)
    seg_count_tree.add(xi+1, 1)

Solution (Python):

import sys
input = sys.stdin.readline
def bsearch(target, min_i, max_i, func):
    
    if func(max_i) <= target:
        return max_i
    if target < func(min_i):
        return None
    index = (max_i + min_i)//2
    while True:
        if func(index) <= target:
            if target < func(index+1):
                return index
            index, min_i = (index+1 + max_i)//2, index+1
            continue
        index, max_i = (index-1 + min_i)//2, index-1

class SegTree:
    def __init__(self, init_val, n, ide_ele, seg_func):
        self.segfunc = seg_func
        self.num = 2**(n-1).bit_length()
        self.ide_ele = ide_ele
        self.seg=[self.ide_ele]*2*self.num
        for i in range(n):
            self.seg[i+self.num-1]=init_val[i]    
        for i in range(self.num-2,-1,-1) :
            self.seg[i]=self.segfunc(self.seg[2*i+1],self.seg[2*i+2]) 
        
    def update(self, k, x):
        k += self.num-1
        self.seg[k] = x
        while k+1:
            k = (k-1)//2
            self.seg[k] = self.segfunc(self.seg[k*2+1],self.seg[k*2+2])
        
    def query(self, p, q):
        if q<=p:
            return self.ide_ele
        p += self.num-1
        q += self.num-2
        res=self.ide_ele
        while q-p>1:
            if p&1 == 0:
                res = self.segfunc(res,self.seg[p])
            if q&1 == 1:
                res = self.segfunc(res,self.seg[q])
                q -= 1
            p = p//2
            q = (q-1)//2
        if p == q:
            res = self.segfunc(res,self.seg[p])
        else:
            res = self.segfunc(self.segfunc(res,self.seg[p]),self.seg[q])
        return res

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

q=int(input())
xs = []
ys = []
qs = dict()
c = 0
for _ in range(q):
    xy = list(map(int,input().split()))
    if len(xy) == 1:
        if c not in qs:
            qs[c] = 0
        qs[c] += 1
    else:
        c += 1
        xs.append(xy[1])
        ys.append(xy[2])

N=len(xs)
sxs=sorted(xs)
zxs = [0]*N
for i, x in enumerate(sorted(range(N), key=lambda x : xs[x])):
    zxs[x] = i

seg_sum_tree = SegTree([0]*N, N, 0, lambda x, y: x+y)

seg_count_tree = Bit(N)

sy = 0
for i in range(c+1):
    if i in qs:
        x1i=bsearch(i//2 + (-1 if i % 2 == 0 else 0) ,0 ,N ,lambda x : seg_count_tree.sum(x))

        if i % 2:
        
            r = sy - seg_sum_tree.query(0, x1i) + \
                   seg_sum_tree.query(x1i+1, N)
        else:
            r = sy - seg_sum_tree.query(0, x1i+1) + \
                   seg_sum_tree.query(x1i+1, N)
        for _ in range(qs[i]):
            print(sxs[x1i], r)
    if i == c:
        break
    x, y = xs[i], ys[i]
    xi = zxs[i]
    sy += y
    seg_sum_tree.update(xi, x)
    seg_count_tree.add(xi+1, 1)

Solution (Python):
import sys,heapq
input = sys.stdin.readline

def main():
    q = int(input())
    
    big = [10**10]
    sma = [10**10]
    sb = 0
    ss = 0

    rr = 0

    for _ in [None]*q:  
        m = tuple(map(int,input().split()))
        
        if m[0] == 1:
            a = m[1]
            rr += m[2]

            if big[0] <= a:
                heapq.heappush(big,a)
                sb += a
            elif -sma[0] >= a:
                heapq.heappush(sma,-a)
                ss += a
            elif len(sma) >= len(big):
                heapq.heappush(sma,-a)
                ss += a
            else:
                heapq.heappush(big,a)
                sb += a
            
            while len(big) > len(sma)+1:
                p = heapq.heappop(big)
                heapq.heappush(sma,-p)
                ss += p
                sb -= p
            
            while len(sma) >  len(big)+1:
                p = -heapq.heappop(sma)
                heapq.heappush(big,p)
                sb += p
                ss -= p
            
        else:
            if len(big) > len(sma):
                mid = big[0]
            else:
                mid = -sma[0]
            
            print(mid,sb-mid*(len(big)-1) + mid*(len(sma)-1) -ss +rr)

if __name__ =="__main__":
    main()

Solution (Python):
import numpy as np
import heapq
Q = int(input())
Qinit = list(map(int,input().split()))
Qlist = [list(map(int,input().split())) for i in range(Q-1)]


intercept = Qinit[2]
left_list = []
cent_list = [Qinit[1]]
right_list = []
left_sum = 0
right_sum = 0

median = Qinit[1]
count = 0

for query in Qlist:
    if query[0] == 1:
        source = query[1]
        intercept += query[2]

        if cent_list == []:
            if median > source:
                heapq.heappush(left_list, -source)
                left_sum += source 
                tmp = -heapq.heappop(left_list)
                left_sum -= tmp 
                cent_list.append(tmp)
            elif right_list[0] > source:
                cent_list.append(source)
            else:
                heapq.heappush(right_list, source)
                tmp = heapq.heappop(right_list)
                right_sum += source - tmp
                cent_list.append(tmp)
            median = cent_list[0]
        else:
            if cent_list[0] < source:
                heapq.heappush(left_list, -cent_list[0])
                left_sum += cent_list[0]
                median = cent_list[0]
                heapq.heappush(right_list, source)
                right_sum += source
                cent_list.remove(cent_list[0])
            else:
                heapq.heappush(left_list, -source)
                left_sum += source
                median = -left_list[0]
                heapq.heappush(right_list, cent_list[0])
                right_sum += cent_list[0]
                cent_list.remove(cent_list[0])
            count += 1

    else:
        minval = np.abs(left_sum-count*median) + np.abs(right_sum-count*median) + intercept
        
        print("{} {}".format(median, minval))

Solution (Python):
from heapq import heappop
from heapq import heappush
q=int(input())
L,R=[],[]
b_sum=0
l_sum,r_sum=0,0
count=0
mid=0
for _ in range(q):
  
  Q=list(map(int,input().split()))
  if Q[0]==1:
    a,b=Q[1],Q[2]
    count+=1
    b_sum+=b
    if count==1:
      mid=a
    elif count%2==0:
      if a<=mid:
        heappush(L,-a)
        l_sum+=a
        heappush(R,mid)
        r_sum+=mid
      else:
        heappush(L,-mid)
        l_sum+=mid
        heappush(R,a)
        r_sum+=a
    else:
      l,r=-heappop(L),heappop(R)
      if a<l:
        heappush(L,-a)
        l_sum+=a-l
        mid=l
        heappush(R,r)
      elif r<a:
        heappush(L,-l)
        mid=r
        heappush(R,a)
        r_sum+=a-r
      else:
        heappush(L,-l)
        mid=a
        heappush(R,r)

  else:
    if count%2==1:
      print(mid,r_sum-l_sum+b_sum)
    else:
      l=-heappop(L)
      print(l,r_sum-l_sum+b_sum)
      heappush(L,-l)

Solution (Python):
import heapq
Q = int(input())
L = []
R = []
B = 0
n = 0
res = 0
for _ in range(1, Q + 1):
  q = input()
  if q == "2":
    print(-L[0], res + B)
  else:
    n += 1
    t, a, b = map(int, q.split())
    B += b
    if len(L) == 0 or a <= -L[0]:
      heapq.heappush(L, -a)
      res += -L[0] - a
    else:
      heapq.heappush(R, a)
      res += a + L[0]
    if len(R) > len(L):
      res -= R[0] + L[0]
      heapq.heappush(L, -heapq.heappop(R))
    elif len(L) - len(R) >= 2:
      heapq.heappush(R, -heapq.heappop(L))

Solution (Python):

import sys
from collections import deque, defaultdict
import copy
import bisect
input=sys.stdin.readline

import math
import heapq

Q = int(input())

q = []
for i in range(Q):
	q.append(list(map(int, input().split())))
	

a_list = []
a_list2 = []
b_sum = 0
a_sum = 0
a_sum_half = 0
length = 0
for i in range(Q):
	if len(q[i]) == 3:
		if len(a_list2) == 0:
			D = True
		else:
			x = heapq.heappop(a_list2)
			heapq.heappush(a_list2,x)
			D = (x <= q[i][1])
		length += 1
		if length % 2 == 0 and not D:
			a_sum_half += q[i][1]
			heapq.heappush(a_list, -q[i][1])
		elif length % 2 == 0 and D:
			heapq.heappush(a_list2, q[i][1])
			x = heapq.heappop(a_list2)
			heapq.heappush(a_list, -x)
			a_sum_half += x
		elif length % 2 == 1 and not D:
			a_sum_half += q[i][1]
			heapq.heappush(a_list, -q[i][1])
			x = heapq.heappop(a_list)
			a_sum_half += x
			heapq.heappush(a_list2, -x)
		else:
			heapq.heappush(a_list2, q[i][1])
		a_sum += q[i][1]
		b_sum += q[i][2]
	else:
		if length % 2 == 0:
			argmin_x = -heapq.heappop(a_list)
			heapq.heappush(a_list, -argmin_x)
		else:
			argmin_x = heapq.heappop(a_list2)
			heapq.heappush(a_list2, argmin_x)
		min_x = argmin_x*(length//2) - a_sum_half
		min_x += (a_sum - a_sum_half) - (argmin_x*(length - length//2))
		min_x += b_sum
		print("{0} {1}".format(argmin_x, min_x))

Solution (Python):
from heapq import heappush, heappop
Q = int(input())
X = [list(map(int, input().split())) for _ in range(Q)]

left_q = []
right_q = []
q_sum = 0
const = 0

for q, *w in X:
    if q == 1:
        x, y = w
        const += y
        
        if len(left_q) == 0 or x <= -left_q[0]:
            heappush(left_q, -x)
            q_sum += -left_q[0] - x
        else:
            heappush(right_q, x)
            q_sum += left_q[0] + x
            
        if len(right_q) > len(left_q):
            q_sum += -right_q[0] - left_q[0]
            heappush(left_q, -heappop(right_q))
        
        if len(left_q) - len(right_q) == 2:
            heappush(right_q, -heappop(left_q))
    else:
        print(-left_q[0], q_sum + const)

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

input = sys.stdin.readline


class median_o1:
    def __init__(self):
        self.count = 0
        self.al = []
        self.ar = []
        self.sl = 0
        self.sr = 0
        self.b = 0

    def update_b(self, b):
        self.b += b

    def insert(self, x):
        if self.count % 2 == 0:
            if self.count == 0:
                heappush(self.al, -x)
                self.sl += x
            else:
                t = heappop(self.ar)
                self.sr -= t
                tl, tr = min(x, t), max(x, t)
                heappush(self.al, -tl)
                self.sl += tl
                heappush(self.ar, tr)
                self.sr += tr
        else:
            t = heappop(self.al)
            t *= (-1)
            self.sl -= t
            tl, tr = min(x, t), max(x, t)
            heappush(self.ar, tr)
            self.sr += tr
            heappush(self.al, -tl)
            self.sl += tl
        self.count += 1

    def get_median(self):
        return -self.al[0]

    def calc_min_value(self):
        
        m = self.get_median()
        return (m * ((self.count + 1) // 2) - self.sl) + (self.sr - m * (self.count // 2)) + self.b


q = int(input())

m = median_o1()

for _ in range(q):
    q_typ, *x = map(int, input().split())
    
    
    if q_typ == 1:
        m.update_b(x[1])
        m.insert(x[0])
    else:
        print(m.get_median(), m.calc_min_value())

Solution (Python):
from heapq import heappop, heappush
import sys
input = sys.stdin.readline


def main():
    Q = int(input())
    y = 0
    lheap = []
    rheap = []
    lsum = 0
    rsum = 0
    for _ in range(Q):
        q = input()
        if q[0] == '1':
            
            _, a, b = map(int, q.split())
            y += b
            if len(lheap) == 0 or -lheap[0] <= a <= rheap[0]:
                heappush(lheap, -a)
                heappush(rheap, a)
                lsum += a
                rsum += a
            elif a < -lheap[0]:
                mov = -heappop(lheap)
                heappush(rheap, mov)
                heappush(lheap, -a)
                heappush(lheap, -a)
                lsum += a*2 - mov
                rsum += mov
            elif rheap[0] < a:
                mov = heappop(rheap)
                heappush(lheap, -mov)
                heappush(rheap, a)
                heappush(rheap, a)
                lsum += mov
                rsum += a*2 - mov
        else:
            
            x = -lheap[0]
            minimum = abs(x * len(lheap) - lsum) // 2 + \
                abs(x * len(rheap) - rsum) // 2 + y
            print(x, minimum)


if __name__ == "__main__":
    main()

Solution (Python):
from heapq import heappop, heappush


def main():
    Q = int(input())
    y = 0
    lheap = []
    rheap = []
    lsum = 0
    rsum = 0
    for _ in range(Q):
        q = input()
        if q[0] == '1':
            
            _, a, b = map(int, q.split())
            y += b
            if len(lheap) == 0 or -lheap[0] <= a <= rheap[0]:
                heappush(lheap, -a)
                heappush(rheap, a)
                lsum += a
                rsum += a
            elif a < -lheap[0]:
                mov = -heappop(lheap)
                heappush(rheap, mov)
                heappush(lheap, -a)
                heappush(lheap, -a)
                lsum += a*2 - mov
                rsum += mov
            elif rheap[0] < a:
                mov = heappop(rheap)
                heappush(lheap, -mov)
                heappush(rheap, a)
                heappush(rheap, a)
                lsum += mov
                rsum += a*2 - mov
        else:
            
            x = -lheap[0]
            minimum = abs(x * len(lheap) - lsum) // 2 + \
                abs(x * len(rheap) - rsum) // 2 + y
            print(x, minimum)


if __name__ == "__main__":
    main()

Solution (Python):
import heapq
q=int(input())
large=[]
small=[]
l_s=0
s_s=0
c=0
ll=0
ls=0
cc=0
for i in range(q):
    s=input()
    if s[0]=='1':
        cc=(cc+1)%2
        _,a,b=map(int,s.split())
        c+=b
        if large==[] and small==[]:
            heapq.heappush(small, -1*a)
            s_s+=a
            ls+=1
        elif a> small[0]*-1:
            heapq.heappush(large, a)
            l_s-=a
            ll+=1
        else:
            x=heapq.heappop(small)
            heapq.heappush(small, -1*a)
            heapq.heappush(large, -1*x)
            s_s=s_s+a+x
            l_s+=x
            ll+=1
        if not (ll<=ls<=ll+1):
            x=heapq.heappop(large)
            heapq.heappush(small, -1*x)
            s_s=s_s+x
            l_s+=x
            ll-=1
            ls+=1
        
    else:
        
        print(small[0]*-1,c-s_s-l_s-small[0]*cc)

Solution (Python):
from heapq import heapify, heappush, heappop
import sys
input = sys.stdin.readline

Q = int(input())
ss = input()
t, A, B = map(int, ss.split())
PQ1, PQ2 = [-A], [A]
fr = to = A
minF = 0
accB = B
anss = []
for _ in range(Q-1):
    ss = input()
    if ss[0] == '1':
        t, A, B = map(int, ss.split())
        if fr <= A <= to:
            heappush(PQ1, -A)
            heappush(PQ2, A)
        elif A < fr:
            p = -heappop(PQ1)
            heappush(PQ2, p)
            heappush(PQ1, -A)
            heappush(PQ1, -A)
            minF += fr-A
        else:
            p = heappop(PQ2)
            heappush(PQ1, -p)
            heappush(PQ2, A)
            heappush(PQ2, A)
            minF += A-to
        fr = -heappop(PQ1)
        to = heappop(PQ2)
        heappush(PQ1, -fr)
        heappush(PQ2, to)
        accB += B
    else:
        anss.append((fr, minF+accB))

print('\n'.join(map(lambda x: str(x[0])+' '+str(x[1]), anss)))

Solution (Python):
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


from heapq import heappush, heappop



upper_asc = []
lower_dsc = []

q = ni()
ans = 0

for i in range(q):
    query = list(map(int, ns().split()))
    if query[0] == 1:
        a, b = query[1], query[2]
        heappush(upper_asc, a)
        heappush(lower_dsc, -a)

        if i == 0:
            ans += b
            continue

        x, y = heappop(upper_asc), -heappop(lower_dsc)

        ans += (y - x) + b

        heappush(upper_asc, y)
        heappush(lower_dsc, -x)

    else:
        print(-lower_dsc[0], ans)

Solution (Python):
from heapq import heappop,heappush
import sys
input = sys.stdin.readline
def resolve():
    N = int(input())
    b = 0
    left = []
    right = []
    sumleft = sumright = mid = 0
    for _ in range(N):
        q = list(map(int,input().split()))
        if q[0]==1:
            a = q[1]
            b = b + q[2]
            if not left or -left[0]>a:
                heappush(left,-a)
                sumleft = sumleft + a
                if (len(left)-len(right))>=2:
                    move = -heappop(left)
                    heappush(right,move)
                    sumleft = sumleft-move
                    sumright = sumright+move
            else:
                heappush(right,a)
                sumright = sumright + a
                if len(left)<len(right):
                    move = heappop(right)
                    heappush(left,-move)
                    sumright = sumright-move
                    sumleft = sumleft+move
            mid = -left[0]
        else:
            ans = (len(left)-len(right))*mid-sumleft+sumright + b
            print(mid,ans)


if __name__ == "__main__":
    resolve()

Solution (Python):
Q = int(input())
qs = [tuple(map(int,input().split())) for i in range(Q)]

import heapq
class Heapq:
    def __init__(self, arr, desc=False):
        if desc:
            arr = [-a for a in arr]
        self.sign = -1 if desc else 1
        self.hq = arr
        heapq.heapify(self.hq)

    def pop(self):
        return heapq.heappop(self.hq) * self.sign

    def push(self, a):
        heapq.heappush(self.hq, a * self.sign)

    def top(self):
        return self.hq[0] * self.sign

lq = Heapq([],True)
rq = Heapq([])
ans = 0

for q in qs:
    if q[0] == 2:
        print(lq.top(), ans)
        continue
    _,a,b = q
    ans += b
    lq.push(a)
    rq.push(a)
    if lq.top() > rq.top():
        l = lq.pop()
        r = rq.pop()
        ans += abs(l-r)
        lq.push(r)
        rq.push(l)

Solution (Python):
import heapq
Q = int(input())
qs = [tuple(map(int,input().split())) for i in range(Q)]

lq = []
rq = []
heapq.heapify(lq)
heapq.heapify(rq)
ans = 0

for q in qs:
    if q[0] == 2:
        print(-lq[0], ans)
        continue
    _,a,b = q
    ans += b
    heapq.heappush(lq, -a)
    heapq.heappush(rq, a)
    if -lq[0] > rq[0]:
        l = -heapq.heappop(lq)
        r = heapq.heappop(rq)
        ans += abs(l-r)
        heapq.heappush(lq,-r)
        heapq.heappush(rq,l)

Solution (Python):
from heapq import heappush, heappushpop

Q = int(input())

b = 0
la = []
sl = 0
ra = []
sr = 0

cnt = 0
for _ in range(Q):
    q = tuple(map(int, input().split()))
    
    if q[0] == 2:
        
        x = -la[0]
        cl = (cnt + 1) // 2
        cr = cnt - cl
        print(x, (cl * x - sl) + (sr - cr * x) + b)
    else:
        
        b += q[2]
        cnt += 1
        if cnt % 2 == 1:
            sr += q[1]
            x = heappushpop(ra, q[1])
            sr -= x
            sl += x
            heappush(la, -x)
        else:
            sl += q[1]
            x = heappushpop(la, -q[1])
            x *= -1
            sl -= x
            sr += x
            heappush(ra, x)

Solution (Python):
from heapq import heappush, heappushpop

Q = int(input())

b = 0
la = []
sl = 0
ra = []
sr = 0

cnt = 0
for _ in range(Q):
    q = tuple(map(int, input().split()))
    
    if q[0] == 2:
        
        x = -la[0]
        cl = (cnt + 1) // 2
        cr = cnt - cl
        print(x, (cl * x - sl) + (sr - cr * x) + b)
    else:
        
        b += q[2]
        cnt += 1
        if cnt % 2 == 1:
            sr += q[1]
            x = heappushpop(ra, q[1])
            sr -= x
            sl += x
            heappush(la, -x)
        else:
            sl += q[1]
            x = heappushpop(la, -q[1])
            x *= -1
            sl -= x
            sr += x
            heappush(ra, x)

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

Q = int(input())

b = 0
la = []
sl = 0
ra = []
sr = 0

cnt = 0
for _ in range(Q):
    q = input().split()
    
    if q[0] == '2':
        
        x = -la[0]
        print(x, ((cnt + 1) // 2 * x - sl) + (sr - cnt // 2 * x) + b)
    else:
        
        x, y = map(int, q[1:])
        b += y
        cnt += 1
        if cnt % 2 == 1:
            sr += x
            x = heappushpop(ra, x)
            sr -= x
            sl += x
            heappush(la, -x)
        else:
            sl += x
            x = heappushpop(la, -x)
            x *= -1
            sl -= x
            sr += x
            heappush(ra, x)

Solution (Python):
def main():
	def Technique_often_used():    
		
		
		
		pass
	def naming_conventions():
		
		
		
		
		pass
	def visual_studio_code_shortcut():    
		
		
		
		pass
	import sys
	from itertools import combinations, permutations 
	from collections import deque, Counter 
	from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest 
	from copy import deepcopy, copy 
	from functools import reduce
	from fractions import gcd 
	def gcds(numbers):
		return reduce(gcd, numbers)
	def lcm(x, y):
		return (x * y) // gcd(x, y)
	def lcms(numbers): 
		return reduce(lcm, numbers, 1)

	
		
		
		
		
		
		
		
		

	R = lambda: map(int, input().split())
	q = int(input())
	queries = [list(R()) for i in range(q)]

	
	

	l = []
	r = []
	ans = 0
	for query in queries:
		if query[0] == 1:
			a = heappushpop(l,-query[1])
			b = heappushpop(r, query[1])
			heappush(l,-b)
			heappush(r,-a)
			ans += abs(b+a) + query[2]
		else:
			print(-l[0],ans)

if __name__ == '__main__':
    main()

Solution (Python):
from itertools import zip_longest
from heapq import heappush, heappop


Q = int(input())

q, a, b = (
    zip_longest(*(map(int, input().split()) for _ in range(Q))) if Q else
    ((), ())
)

INF = 10**18
L = []
R = []
heappush(L, INF)
heappush(R, INF)
S = 0
res = []

for i, A, B in zip(q, a, b):
    if i == 1:
        S += B
        if len(L) == len(R):
            if A < -L[0]:
                S += -L[0] - A
                heappush(L, -A)
            elif A <= R[0]:
                heappush(L, -A)
            else:
                S += A - R[0]
                t = heappop(R)
                heappush(L, -t)
                heappush(R, A)
        else:
            if A < -L[0]:
                S += -L[0] - A
                t = -heappop(L)
                heappush(R, t)
                heappush(L, -A)
            else:
                S += A - (-L[0])
                heappush(R, A)
    else:
        res.append((-L[0], S))

ans = '\n'.join(
    ' '.join(map(str, r))
    for r in res
)


print(ans)

Solution (Python):
def Technique_often_used():    
    
    
    
    pass
def naming_conventions():
    
    
    
    
    pass
def visual_studio_code_shortcut():    
    
    
    
    pass
import sys
from itertools import combinations, permutations 
from collections import deque, Counter 
from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest 
from copy import deepcopy, copy 
from functools import reduce
from fractions import gcd 
def gcds(numbers):
    return reduce(gcd, numbers)
def lcm(x, y):
    return (x * y) // gcd(x, y)
def lcms(numbers): 
    return reduce(lcm, numbers, 1)


    
    
    
    
    
    
    
    

R = lambda: map(int, input().split())
q = int(input())
queries = [list(R()) for i in range(q)]




l = []
r = []
ans = 0
for query in queries:
    if query[0] == 1:
        a = heappushpop(l,-query[1])
        b = heappushpop(r, query[1])
        heappush(l,-b)
        heappush(r,-a)
        ans += abs(b+a) + query[2]
    else:
        print(-l[0],ans)

Solution (Python):
from heapq import heappop,heappush
l = []
r = []
Q = int(input())
ans = 0
l_s = 0
r_s = 0
_,m,b = map(int,input().split())
ans += b
n = 0
for i in range(1,Q):
    s = input()
    if s != '2':
        _,a,b = map(int,s.split())
        ans += b
        if n == 0:
            c,d = sorted([m,a])
            heappush(l,-c)
            heappush(r,d)
            l_s += c
            r_s += d
            m1 = c
        else:
            c,d = heappop(l),heappop(r)
            l_s += c
            r_s -= d
            c = -c
            if n%2 == 1:
                c,m,d = sorted([a,c,d])
                heappush(l,-c)
                heappush(r,d)
                l_s += c
                r_s += d
            else:
                c,m1,m2,d = sorted([m,a,c,d])
                heappush(l,-c)
                heappush(l,-m1)
                heappush(r,d)
                heappush(r,m2)
                l_s += c
                r_s += d
                l_s += m1
                r_s += m2
        n += 1
    else:
        if n == 0:
            print('{} {}'.format(m,ans))
        elif n == 1:
            print('{} {}'.format(m1,r_s-l_s+ans))
        elif n%2 == 0:
            print('{} {}'.format(m,r_s-l_s+ans))
        else:
            print('{} {}'.format(m1,r_s-l_s+ans))