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



Sample input:
4
1 4 2
2
1 1 -8
2


Sample output:
4 2
1 -3

Submission status: Runtime Error

Solution code:
q=int(input())
f=0
a=[]
for _ in range(q):
    que=list(map(int,input().split()))
    if que[0]==2:
        a=sorted(a)
        l=len(a)
        if l%2==0:
            key=a[l//2-1]
            for i in range(l):
                f+=abs(key-a[i])
        else:
            key=a[l//2]
            for i in range(l):
                f+=abs(key-a[i])
        print(key,f)
        a=[]
    else:
        a.append(que[1])
        f=f+que[2]

Submission status: Runtime Error

Solution code:
def absolute_minima():
    Q = int(input())

    L, R = [], []
    sL, sR = 0, 0
    M = None
    B = 0
    N = 0

    for _ in range(Q):
        query = map(int, input().split())

        if query[0] == 1:
            _, a, b = query
            N += 1
            B += b

            if M == None:
                M = a
            else:
                if M < a:
                    R.append(a)
                    sR += a
                else:
                    L.append(a)
                    sL += a

                while len(R)-len(L) > 1:
                    L.append(M)
                    sL += M

                    M = R[0]

                    R.remove(M)
                    sR -= M

                while len(L) > len(R):
                    R.append(M)
                    sR += M

                    M = L[0]

                    L.remove(M)
                    sL -= M

        else:
            s = - sL + sR + B
            if N & 1 == 0:
                s -= M
            print(M, s)


if __name__ == "__main__":
    absolute_minima()

Submission status: Runtime Error

Solution code:
Q = int(input())
A = []
n = 0
B = 0
def insert(a, lower, upper):
  if upper - lower == 1:
    A = A[:upper] + [a] + A[upper:]
  else:
    mid = (lower + upper) // 2
    if A[mid] == a:
      A = A[:mid] + [a] + A[mid:]
    elif A[mid] > a:
      insert(a, lower, mid)
    else:
      insert(a, mid, upper)
for q in range(Q):
  q = [int(x) for x in input().split()]
  if q[0] == 1:
    if len(A) == 0:
      A = [q[1]]
    elif A[0] >= q[1]:
      A = [q[1]] + A
    elif A[-1] <= q[1]:
      A = A + [q[1]]
    else:
      insert(q[1], 0, len(A))
    B += q[2]
  else:
    if len(A) == 0:
      print(0, 0)
    else:
      k = (len(A) - 1) // 2
      print(A[k], sum([abs(a - A[k]) for a in A]) + B)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
Q = int(input())
m, M, s1, s2, q1 = 10 ** 9 + 1, -(10 ** 9 + 1), 0, 0, 0
x = []
for i in range(Q):
    q = list(map(int, input().split()))
    if q[0] == 1:
        if q[1] < m:
            m = q[1]
            s1 += ((M - m) // 2 - mid) * q1
            mid = (M - m) // 2
        if q[1] > M:
            M = q[1]
            s1 += ((M - m) // 2 - mid) * q1
            mid = (M - m) // 2
        s1 += abs(q[1] - mid)
        s2 += q[2]
        q1 += 1
    else:
        print(m, s1 + s2)

Submission status: Runtime Error

Solution code:
q=int(input())
mi=1e12
z=[]
za=0
c=0
b=0
for i in range(q):
    ans=0
    a=list(map(int,input().split()))
    if len(a)==3:
        c+=1
        z.append(a[1])
        b+=a[2]
        if mi!=1e12 and mi<=a[1]:
            za+=abs(a[1]-mi)
        elif m!=1e12:
            za+=(c-1)*(a[1]-mi)
        mi=min(mi,a[1])
    else:
        print(mi,end=' ')
        print(za+b)

Submission status: Runtime Error

Solution code:
import statistics


# Binary Indexed Tree (Fenwick Tree)
class BIT():

    def __init__(self, n):
        '''
        n = 要素数
        添字は i = 0 ~ n-1 となる
        '''
        self.n = n
        self.bit = [0] * (n + 1)

    def add(self, i, x):
        '''i番目の要素にxを加算する'''
        i = i + 1
        while i <= self.n:
            self.bit[i] += x
            i += i & -i

    def _sum(self, i):
        s = 0
        while i > 0:
            s += self.bit[i]
            i -= i & -i
        return s

    def get(self, i, j):
        '''[i, j)の和を求める'''
        return self._sum(j) - self._sum(i)


def compress(list1):
    list2 = sorted(set(list1))
    memo = {value : index for index, value in enumerate(list2)}
    #for i in range(len(list1)):
    #    list1[i] = memo[list1[i]]
    return memo, len(list2)

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

#座圧する
li1 = []
for i in range(q):
    if info[i][0] == 1:
        li1.append(info[i][1])

memo, len_memo = compress(li1)


#値を管理するBIT
bit = BIT(len_memo)
#要素数を管理するBITに
bit1 = BIT(len_memo)

x = []
b = 0
for i in range(q):
    if info[i][0] == 1:
        bit.add(memo[info[i][1]], info[i][1])
        bit1.add(memo[info[i][1]], 1)
        b += info[i][2]
        x.append(info[i][1])
    if info[i][0] == 2:
        median_x = statistics.median_low(x)
        sum1 = bit.get(0, memo[median_x])
        num1 = bit1.get(0, memo[median_x])
        sum2 = bit.get(memo[median_x], len_memo)
        num2 = bit1.get(memo[median_x], len_memo)
        #print(median_x, sum1,num1, sum2, num2)
        print(median_x, abs(num1*median_x - sum1) + abs(num2*median_x - sum2) + b)

Submission status: Runtime Error

Solution code:
q=int(input())
mi=-1000000000000000000
z=[]
b=0
for i in range(q):
   ans=0
    a=list(map(int,input().split()))
    if len(a)==3:
        z.append(a[1])
        b+=a[2]
        mi=max(mi,a[1])
    else:
        for j in z:
            ans+=abs(j-mi)
        print(mi,end=' ')
        print(ans)

Submission status: Runtime Error

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

q = ni()

lh = []
hh = []
ln = 0
hn = 0
ls = 0
hs = 0
off = 0
for z in range(q):
    line = na()
    if line[0] == 1:
        _, a, b = line
        off += b

        if len(lh) > 0:
            mid = -lh[0]
            if a <= mid:
                heapq.heappush(lh, -a)
                ls += a
                ln += 1
            else:
                heapq.heappush(hh, a)
                hs += a
                hn += 1
        else:
            heapq.heappush(lh, -a)
            ls += a
            ln += 1

        while ln < hn:
            mx = heapq.heappop(hh)
            hs -= mx
            hn -= 1
            ls += mx
            ln += 1
            heapq.heappush(lh, -mx)

        while ln > hn + (ln+hn)%2:
            mx = -heapq.heappop(lh)
            ls -= mx
            ln -= 1
            heapq.heappush(hh, mx)
            hs += mx
            hn += 1
    else:
        lls = -lh[0]*ln - ls
        hhs = hs - (-lh[0]*hn)
        print(-lh[0], lls+hhs+off)

Submission status: Runtime Error

Solution code:
ql=int(input())
es=[]
B=0
for qi in range(ql):
  q=map(int,input().split())
  if q==2:
    re=B
    ei=len(es)//2
    emin=es[ei]
    for e in es:
      re += abs(e-emin)
    print(re)
  else:
    _,e,b=map(q)
    es.append(e)
    e.sort()
    B+=b