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: Wrong Answer

Solution code:
Q = int(input())
query = []
cal = []
intc = []
count = 0

for _ in range(Q):
    a = list(map(int, input().split()))
    if a[0]==1:
        intc.append(a[2])
        query.append(a[1])
        count += 1

    else:
        cal.append(count)

for c in cal:
    if c==1:
        print(query[0],intc[0])

    else:
        q = query[:c]
        q.sort()
        if c==2:
            print(q[0],sum(intc[:2])+q[1]-q[0])
        else:
            minx = int((c-1)/2)
            ans = -sum(q[:minx])+sum(q[minx+1:])+sum(intc[:c])
            print(q[minx],ans)

Submission status: Wrong Answer

Solution code:
import heapq
q=int(input())
L=[10**10]
R=[10**10]
heapq.heapify(L)
heapq.heapify(R)
fx=0
for i in range(q):
  Q=list(map(int,input().split()))
  if Q[0]==1:
    a,b=Q[1],Q[2]
    l=-heapq.heappop(L)
    r=heapq.heappop(R)
    if a<l:
      heapq.heappush(L,-a)
      heapq.heappush(L,-a)
      heapq.heappush(R,-l)
      heapq.heappush(R,r)
      fx+=l-a
    elif l<=a<=r:
      heapq.heappush(L,-l)
      heapq.heappush(L,-a)
      heapq.heappush(R,a)
      heapq.heappush(R,r)
    else:
      heapq.heappush(L,-l)
      heapq.heappush(L,-r)
      heapq.heappush(R,a)
      heapq.heappush(R,a)
      fx+=a-r
    x=-L[0]
    fx+=b
  else:
    print(x,fx)

Submission status: Wrong Answer

Solution code:
Q=int(input())
q,a,b,fx,x=[],[],0,0,0
for i in range(Q):
  q.append(list(map(int,input().split())))
  if q[i][0]==2:
    fx+=b
    print(x,fx)
  else:
    a.append(q[i][1])
    b+=q[i][2]
    n=len(a)
    na=a[-1]
    if n%2==0:
      r=a[n//2-1]
      a.sort()
      if na<r:
        x=a[n//2-1]
        fx+=x-na
      elif na==r:
        x=a[n//2-1]
      else:
        x=r
        fx+=na-r
    else:
      r=[a[n//2-1],a[n//2]]
      a.sort()
      if na<r[0]:
        x=a[n//2-1]
        fx+=r[0]-na
      elif r[0]<=na<=r[1]:
        x=r[0]
        fx+=na-r[0]
      else:
        x=a[n//2]
        fx+=na-x

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    from heapq import heappush
    A = []
    ans_a = 0
    ans_b = 0
    ans_x = 0
    for i in range(int(input())):
        query = input()
        if query[0] == "1":
            _, a, b = map(int, query.split())
            len_a = len(A)
            if len_a != 0:
                if len_a % 2 == 0:
                    if a <= A[len_a//2-1]:
                        ans_a += A[len_a//2-1] - a
                    elif a >= A[len_a//2]:
                        ans_a += a - A[len_a//2]
                else:
                    ans_a += abs(a - A[len_a//2])
            heappush(A, a)
            len_a = len(A)
            if len_a % 2 == 0:
                ans_x = A[len_a // 2 - 1]
            else:
                ans_x = A[len_a // 2]
            ans_b += b
        else:
            print(ans_x, ans_a + ans_b)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
q = int(input())
ans = [0, 0]
import heapq
p1 = []
p2 = []
heapq.heapify(p1)
heapq.heapify(p2)
sum_b = 0
sum_p1 = 0
sum_p2 = 0
for _ in range(q):
    ql = list(map(int,input().split()))
    if ql[0] == 2:
        if len(p1) == len(p2):
            print(-p2[0], sum_p1 - len(p1) * -p2[0] + len(p2) * -p2[0] - sum_p2 + sum_b)
        else:
            print(p1[0], sum_p1 - len(p1) * p1[0] + len(p2) * p1[0] - sum_p2 + sum_b)
    else:
        sum_b += ql[2]
        if len(p1) == 0:
            heapq.heappush(p1, ql[1])
            sum_p1 += ql[1]
        elif p1[0] <= ql[1]:
            heapq.heappush(p1, ql[1])
            sum_p1 += ql[1]
        else:
            heapq.heappush(p2, -ql[1])
            sum_p2 += -ql[1]
        if len(p1) < len(p2):
            k = heapq.heappop(p2)
            heapq.heappush(p1, -k)
            sum_p2 -= k
            sum_p1 += k
        if len(p1) - 1 > len(p2):
            k = heapq.heappop(p1)
            heapq.heappush(p2, -k)
            sum_p1 -= k
            sum_p2 += k

Submission status: Wrong Answer

Solution code:
q = int(input())
ans = [0, 0]
import heapq
p1 = []
p2 = []
heapq.heapify(p1)
heapq.heapify(p2)
sum_b = 0
sum_p1 = 0
sum_p2 = 0
for _ in range(q):
    ql = list(map(int,input().split()))
    if ql[0] == 2:
        if len(p1) == len(p2):
            print(-p2[0], sum_p1 - len(p1) * p1[0] + len(p2) * p1[0] - sum_p2 + sum_b)
        else:
            print(p1[0], sum_p1 - len(p1) * p1[0] + len(p2) * p1[0] - sum_p2 + sum_b)
    else:
        sum_b += ql[2]
        if len(p1) == 0:
            heapq.heappush(p1, ql[1])
            sum_p1 += ql[1]
        elif p1[0] <= ql[1]:
            heapq.heappush(p1, ql[1])
            sum_p1 += ql[1]
        else:
            heapq.heappush(p2, -ql[1])
            sum_p2 += ql[1]
        if len(p1) < len(p2):
            k = heapq.heappop(p2)
            heapq.heappush(p1, -k)
            sum_p2 -= k
            sum_p1 += k
        if len(p1) - 1 > len(p2):
            k = heapq.heappop(p1)
            heapq.heappush(p2, -k)
            sum_p1 -= k
            sum_p2 += k

Submission status: Wrong Answer

Solution code:
q = int(input())
ans = [0, 0]
import heapq
p1 = []
p2 = []
heapq.heapify(p1)
heapq.heapify(p2)
sum_b = 0
sum_p1 = 0
sum_p2 = 0
for _ in range(q):
    ql = list(map(int,input().split()))
    if ql[0] == 2:
        print(p1[0], sum_p1 - len(p1) * p1[0] + len(p2) * p1[0] - sum_p2 + sum_b)
    else:
        sum_b += ql[2]
        if len(p1) == 0:
            heapq.heappush(p1, ql[1])
            sum_p1 += ql[1]
        elif p1[0] <= ql[1]:
            heapq.heappush(p1, ql[1])
            sum_p1 += ql[1]
        else:
            heapq.heappush(p2, -ql[1])
            sum_p2 += ql[1]
        if len(p1) < len(p2):
            k = heapq.heappop(p2)
            heapq.heappush(p1, -k)
            sum_p2 -= k
            sum_p1 += k
        if len(p1) - 1 > len(p2):
            k = heapq.heappop(p1)
            heapq.heappush(p2, -k)
            sum_p1 -= k
            sum_p2 += k

Submission status: Wrong Answer

Solution code:
Q = int(input())

Query = [list(input().split()) for i in range(Q)]

m = 0
M = 0
ret = 0
x = 0
f = 0
s = []
for query in Query:
    if query[0] == "1":
        a = int(query[1])
        b = int(query[2])

        ret += b
        s.append(a)
        if len(s) == 3:
            s.sort()
            x = s[1]
            f = abs(x-s[0]) + abs(x-s[2])
            m = s[0]
            ans = x
        if len(s) > 3:
            f += abs(x-a)
            if s[0] > a:
                ans = s[0]
            else:
                ans = min(ans, a)

    else:
        if len(s) == 1:
            print(s[0], ret)
        else:
            if len(s) == 2:
                print(min(s[0], s[1]), abs(s[0] - s[1]) + ret)
            else:
                print(ans, f + ret)

Submission status: Wrong Answer

Solution code:
Q = int(input())
qs = [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

    def size(self):
        return len(self.hq)

lq = Heapq([], True)
rq = Heapq([], False)

ofs = 0
for q in qs:
    if q[0] == '2':
        print(lq.top(), ofs)
        continue
    _,a,b, = q
    a,b = int(a),int(b)
    ofs += b
    if lq.size():
        if rq.size() == 0:
            mn = mx = lq.top()
        else:
            mn,mx = lq.top(), rq.top()
        if a < mn:
            ofs += mn - a
        elif a > mx:
            ofs += a - mx
    if lq.size() == rq.size():
        lq.push(a)
    else:
        rq.push(a)
    if rq.size() and lq.top() > rq.top():
        l,r = lq.pop(), rq.pop()
        lq.push(r)
        rq.push(l)

Submission status: Wrong Answer

Solution code:
Q = int(input())

Query = [list(input().split()) for i in range(Q)]

m = 0
M = 0
ret = 0
x = 0
f = 0
s = []
for query in Query:
    if query[0] == "1":
        a = int(query[1])
        b = int(query[2])

        ret += b
        s.append(a)
        if m > a:
            m = a
        if M < a:
            M = a
        if len(s) == 3:
            s.sort()
            x = s[1]
            f = abs(x-s[0]) + abs(x-s[2])
        if len(s) > 3:
            f += abs(x-a)

    else:
        if len(s) == 1:
            print(s[0], ret)
        else:
            if len(s) == 2:
                print(s[0], abs(s[0] - s[1]) + ret)
            else:
                print(x, f + ret)

Submission status: Wrong Answer

Solution code:
Q = int(input())
qs = [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

    def size(self):
        return len(self.hq)

lq = Heapq([], True)
rq = Heapq([], False)

ofs = 0
for q in qs:
    if q[0] == '2':
        print(lq.top(), ofs)
        continue
    _,a,b, = q
    a,b = int(a),int(b)
    ofs += b
    if lq.size() and a < lq.top():
        ofs += lq.top() - a
    elif rq.size() and a > rq.top():
        ofs += a - rq.top()
    if lq.size() == rq.size():
        lq.push(a)
    else:
        rq.push(a)
    if rq.size() and lq.top() > rq.top():
        l,r = lq.pop(), rq.pop()
        lq.push(r)
        rq.push(l)

Submission status: Wrong Answer

Solution code:
Q = int(input())
qs = [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

    def size(self):
        return len(self.hq)

lq = Heapq([], True)
rq = Heapq([], False)

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

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop

q = int(input())

# PQ1に小さい方→PQ1で欲しいのは最大値→負の値でpush
# PQ2に大きい方→PQ2で欲しいのは最小値→そのままpush
# sizeが同じ時はPQ1に入れるようにしておけば常にクエリで取り出すのはPQ1
PQ1 = []
pqsize1 = 0
PQ2 = []
pqsize2 = 0
seppen = 0
a_sum_small = 0
a_sum_big = 0
b_sum = 0
for _ in range(q):
    a = input()
    if a[0] == "1":
        p, q, r = map(int, a.split())
        b_sum += r
        if pqsize1 == 0:
            heappush(PQ1, -q)
            pqsize1 += 1
            a_sum_small += q
        elif pqsize2 == 0:
            temp = heappop(PQ1)
            temp *= -1
            if temp > q:
                heappush(PQ1, -q)
                heappush(PQ2, temp)
                pqsize2 += 1
                a_sum_small = q
                a_sum_big = temp
            else:
                heappush(PQ1, -temp)
                heappush(PQ2, p)
                pqsize2 += 1
                a_sum_small = temp
                a_sum_big = q
        else:
            smaller = heappop(PQ1)
            smaller *= -1
            bigger = heappop(PQ2)
            if q < smaller:
                if pqsize2 < pqsize1:
                    heappush(PQ1, -q)
                    heappush(PQ2, smaller)
                    heappush(PQ2, bigger)
                    pqsize2 += 1
                    a_sum_small += -smaller + q
                    a_sum_big += smaller
                else:
                    heappush(PQ1, -q)
                    heappush(PQ1, -smaller)
                    heappush(PQ2, bigger)
                    pqsize1 += 1
                    a_sum_small += q
            elif q > bigger:
                if pqsize2 < pqsize1:
                    heappush(PQ1, -smaller)
                    heappush(PQ2, bigger)
                    heappush(PQ2, q)
                    pqsize2 += 1
                    a_sum_big += q
                else:
                    heappush(PQ1, -smaller)
                    heappush(PQ1, -bigger)
                    heappush(PQ2, q)
                    pqsize1 += 1
                    a_sum_small += bigger
                    a_sum_big += q - bigger
            else:
                if pqsize2 < pqsize1:
                    heappush(PQ1, -smaller)
                    heappush(PQ2, q)
                    heappush(PQ2, bigger)
                    pqsize2 += 1
                    a_sum_big += q
                else:
                    heappush(PQ1, -smaller)
                    heappush(PQ1, -q)
                    heappush(PQ2, bigger)
                    pqsize1 += 1
                    a_sum_small += q
    else:
        res = 0
        res += b_sum
        chuo = heappop(PQ1)
        chuo *= -1
        res += pqsize1 * chuo - a_sum_small
        res += a_sum_big - pqsize2 * chuo
        heappush(PQ1, -chuo)
        print(chuo, res)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop

q = int(input())

# PQ1に小さい方→PQ1で欲しいのは最大値→負の値でpush
# PQ2に大きい方→PQ2で欲しいのは最小値→そのままpush
# sizeが同じ時はPQ1に入れるようにしておけば常にクエリで取り出すのはPQ1
PQ1 = []
pqsize1 = 0
PQ2 = []
pqsize2 = 0
seppen = 0
a_sum_small = 0
a_sum_big = 0
b_sum = 0
for _ in range(q):
    a = input()
    if a[0] == "1":
        p, q, r = map(int, a.split())
        b_sum += r
        if pqsize1 == 0:
            heappush(PQ1, -q)
            pqsize1 += 1
            a_sum_small += q
        elif pqsize2 == 0:
            temp = heappop(PQ1)
            temp *= -1
            if temp > q:
                heappush(PQ1, -q)
                heappush(PQ2, temp)
                pqsize2 += 1
                a_sum_small = q
                a_sum_big = temp
            else:
                heappush(PQ1, -temp)
                heappush(PQ2, p)
                pqsize2 += 1
                a_sum_small = temp
                a_sum_big = q
        else:
            smaller = heappop(PQ1)
            smaller *= -1
            bigger = heappop(PQ2)
            if q < smaller:
                if pqsize2 < pqsize1:
                    heappush(PQ1, -q)
                    heappush(PQ2, smaller)
                    heappush(PQ2, bigger)
                    pqsize2 += 1
                    a_sum_small += -smaller + q
                    a_sum_big += smaller
                else:
                    heappush(PQ1, -q)
                    heappush(PQ1, -smaller)
                    heappush(PQ2, bigger)
                    pqsize2 += 1
                    a_sum_small += q
            elif q > bigger:
                if pqsize2 < pqsize1:
                    heappush(PQ1, -smaller)
                    heappush(PQ2, bigger)
                    heappush(PQ2, q)
                    pqsize2 += 1
                    a_sum_big += q
                else:
                    heappush(PQ1, -smaller)
                    heappush(PQ1, -bigger)
                    heappush(PQ2, q)
                    pqsize2 += 1
                    a_sum_small += bigger
                    a_sum_big += q - bigger
            else:
                if pqsize2 < pqsize1:
                    heappush(PQ1, -smaller)
                    heappush(PQ2, q)
                    heappush(PQ2, bigger)
                    pqsize2 += 1
                    a_sum_big += q
                else:
                    heappush(PQ1, -smaller)
                    heappush(PQ1, -q)
                    heappush(PQ2, bigger)
                    pqsize2 += 1
                    a_sum_small += q
    else:
        res = 0
        res += b_sum
        chuo = heappop(PQ1)
        chuo *= -1
        res += pqsize1 * chuo - a_sum_small
        res += a_sum_big - pqsize2 * chuo
        heappush(PQ1, -chuo)
        print(chuo, res)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)


class BinaryIndexTree:  # 1-indexed
    def __init__(self, N):
        """
        INPUT
        N [int] -> 全部0で初期化
        N [list] -> そのまま初期化
        """
        if isinstance(N, int):
            self.N = N
            self.depth = N.bit_length()
            self.tree = [0] * (N + 1)
            self.elem = [0] * (N + 1)
        elif isinstance(N, list):
            self.N = len(N)
            self.depth = self.N.bit_length()
            self.tree = [0] + N
            self.elem = [0] + N
            self._init()
        else:
            raise "INVALID INPUT: input must be int or list"

    def _init(self):
        size = self.N + 1
        for i in range(1, self.N):
            if i + (i & -i) > size:
                continue
            self.tree[i + (i & -i)] += self.tree[i]

    def add(self, i, x):
        self.elem[i] += x
        while i <= self.N:
            self.tree[i] += x
            i += i & -i

    def sum(self, i):
        res = 0
        while i > 0:
            res += self.tree[i]
            i -= i & -i
        return res

    def lower_bound(self, val):
        if val <= 0:
            return 0
        i = 0
        k = 1 << self.depth
        while k:
            if i + k <= self.N and self.tree[i + k] < val:
                val -= self.tree[i + k]
                i += k
            k >>= 1
        return i + 1


if __name__ == "__main__":
    Q = int(input())
    inf = 10**10
    cand = {-inf, inf}
    query = []
    for _ in range(Q):
        T, *arg = map(int, input().split())
        if T == 1:
            cand.add(arg[0])
        query.append(arg)

    cand = sorted(list(cand))
    idx = {a: i for i, a in enumerate(cand, start=1)}

    size = len(cand) + 10
    bit_val = BinaryIndexTree(size)
    bit_pos = BinaryIndexTree(size)

    cnt = 0
    B = 0
    for q in query:
        if q:  # f(x)更新
            cnt += 1
            a, b = q
            B += b
            i = idx[a]
            bit_pos.add(i, 1)
            bit_val.add(i, a)
        else:  # 解を求める
            pos = bit_pos.lower_bound((cnt + 1) // 2)
            if cnt % 2 == 0:
                A = bit_val.sum(size) - 2 * bit_val.sum(pos)
            else:
                A = bit_val.sum(size) - bit_val.sum(pos)
                A -= bit_val.sum(pos - 1)
            X = cand[pos - 1]
            print(X, A + B)

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs,heapq as hq
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
  return list(map(int,input().split()))
def m(): #複数文字
  return map(int,input().split())
def onem(): #Nとかの取得
  return int(input())
def s(x): #圧縮
  a = []
  if len(x) == 0:
    return []
  aa = x[0]
  su = 1
  for i in range(len(x)-1):
    if aa != x[i+1]:
      a.append([aa,su])
      aa = x[i+1]
      su = 1
    else:
      su += 1
  a.append([aa,su])
  return a
def jo(x): #listをスペースごとに分ける
  return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
  return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
  ll = 0
  rr = r
  while True:
    mid = (ll+rr)//2

  if rr == mid:
    return ll
  if (ここに評価入れる):
    rr = mid
  else:
    ll = mid+1
"""

q = onem()

b = 0

AR = []
AL = []

ans = 0



for i in range(q):
  a = l()
  if len(a) == 1:
    if len(AR) == len(AL):
      print(min(AR[0]*-1,AL[0]),ans + b)
    else:
      print(AR[0]*-1,ans + b)

  else:
    b += a[2]
    if AR == []:
      AR.append(-a[1])
    else:
      if len(AR) == len(AL):
        if AL[0] < a[1]:
          ans += a[1]-AL[0]
          hq.heappush(AL,a[1])
          ppp = hq.heappop(AL)
          hq.heappush(AR,ppp*-1)
        else:
          ans += (-1*AR[0] - a[1])
          hq.heappush(AR,a[1]*-1)

      else:
        if (AR[0]* -1) > a[1]:
          ans += (-1*AR[0] - a[1])
          hq.heappush(AR,a[1]*-1)
          ppp = hq.heappop(AR)*-1
          hq.heappush(AL,ppp)
        else:
          ans += (AR[0] + a[1])
          hq.heappush(AL,a[1])

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs,heapq as hq
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
  return list(map(int,input().split()))
def m(): #複数文字
  return map(int,input().split())
def onem(): #Nとかの取得
  return int(input())
def s(x): #圧縮
  a = []
  if len(x) == 0:
    return []
  aa = x[0]
  su = 1
  for i in range(len(x)-1):
    if aa != x[i+1]:
      a.append([aa,su])
      aa = x[i+1]
      su = 1
    else:
      su += 1
  a.append([aa,su])
  return a
def jo(x): #listをスペースごとに分ける
  return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
  return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
  ll = 0
  rr = r
  while True:
    mid = (ll+rr)//2

  if rr == mid:
    return ll
  if (ここに評価入れる):
    rr = mid
  else:
    ll = mid+1
"""

q = onem()

b = 0

AR = []
AL = []

ans = 0



for i in range(q):
  a = l()
  if len(a) == 1:
    if len(AR) == len(AL):
      """
      if -1*AR[0] * AL[0] > 0:
        print(min(AR[0]*-1,AL[0]),ans + b)
      else:
        print(0,ans + b) 
      #print(max(0,min(AR[0]*-1,AL[0])),ans + b)
      """
      print(min(AR[0]*-1,AL[0]),ans + b)
    else:
      print(AR[0]*-1,ans + b)

  else:
    b += a[2]
    if AR == []:
      AR.append(-a[1])
    elif AL == []:
      ans += abs(a[1] + AR[0])
      AL.append(a[1])
    else:
      if len(AR) == len(AL):
        if AL[0] < a[1]:
          ans += a[1]-AL[0]
          hq.heappush(AL,a[1])
          ppp = hq.heappop(AL)
          hq.heappush(AR,ppp*-1)
        else:
          ans += (-1*AR[0] - a[1])
          hq.heappush(AR,a[1]*-1)

      else:
        if (AR[0]* -1) > a[1]:
          hq.heappush(AR,a[1]*-1)
          ppp = hq.heappop(AR)*-1
          hq.heappush(AL,ppp)
        else:
          ans += (1*AR[0] + a[1])
          hq.heappush(AL,a[1])

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs,heapq as hq
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
  return list(map(int,input().split()))
def m(): #複数文字
  return map(int,input().split())
def onem(): #Nとかの取得
  return int(input())
def s(x): #圧縮
  a = []
  if len(x) == 0:
    return []
  aa = x[0]
  su = 1
  for i in range(len(x)-1):
    if aa != x[i+1]:
      a.append([aa,su])
      aa = x[i+1]
      su = 1
    else:
      su += 1
  a.append([aa,su])
  return a
def jo(x): #listをスペースごとに分ける
  return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
  return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
  ll = 0
  rr = r
  while True:
    mid = (ll+rr)//2

  if rr == mid:
    return ll
  if (ここに評価入れる):
    rr = mid
  else:
    ll = mid+1
"""

q = onem()

b = 0

AR = []
AL = []

ans = 0



for i in range(q):
  a = l()
  if len(a) == 1:
    if len(AR) == len(AL):
      print(min(0,min(AR[0],AL[0])),ans + b)
    else:
      print(AR[0],ans + b)

  else:
    a[1] *= -1
    b += a[2]
    if AR == []:
      AR.append(-a[1])
    elif AL == []:
      ans += abs(a[1] + AR[0])
      AL.append(a[1])
    else:
      if len(AR) == len(AL):
        if AL[0] < a[1]:
          ans += a[1]-AL[0]
          hq.heappush(AL,a[1])
          ppp = hq.heappop(AL)
          hq.heappush(AR,ppp*-1)
        else:
          ans += (-1*AR[0] - a[1])
          hq.heappush(AR,a[1]*-1)

      else:
        if (AR[0]* -1) > a[1]:
          hq.heappush(AR,a[1]*-1)
          ppp = hq.heappop(AR)*-1
          hq.heappush(AL,ppp)
        else:
          ans += (1*AR[0] + a[1])
          hq.heappush(AL,a[1])

Submission status: Wrong Answer

Solution code:
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

def bsearch(mn, mx, func):
    #func(i)=True を満たす最大のi (mn<=i<mx)
    idx = (mx + mn)//2
    while mx-mn>1:
        if func(idx):
            idx, mn = (idx + mx)//2, idx
            continue
        idx, mx = (idx + mn)//2, idx
    return idx

import sys;input=sys.stdin.readline
Q, = map(int, input().split())
qs = []
c = 0
j = 0
for _ in range(Q):
    q = input().split()
    if len(q) == 1:
        c += 1
    else:
        a, b = int(q[1]), int(q[2])
        qs.append((a, b, c, j))
        c = 0
        j+=1
if c:
    qs.append((a, b, c, j))

m = j
qqs = sorted(qs, key=lambda x:x[0])
j2i = dict()
i2a = dict()
for i, (a, b, c, j) in enumerate(qqs):
    j2i[j] = i+1
    i2a[i+1] = a

m = len(j2i)
st = Bit(m)
st_sum = Bit(m)
asum=bsum=0
#print(i2a)
#print(j2i)
for a, b, c, i in qs:
    if c:
        j = bsearch(0, m+1, lambda x: st.sum(x)<ci)
#        print(st_sum.sum(j+1))
        if not i%2:
            print(i2a[j+1], -2*st_sum.sum(j+1)+asum+bsum)
        else:
            print(i2a[j+1], -st_sum.sum(j)+(asum-st_sum.sum(j+1))+bsum)
    asum += a
    bsum += b
    st.add(j2i[i], 1)
    st_sum.add(j2i[i], a)
    ci = -(-(i+1)//2)

Submission status: Wrong Answer

Solution code:
from heapq import *
import sys;input=sys.stdin.readline
Q, = map(int, input().split())
qs = []
c = 0
for i in range(Q):
    q = input().split()
    if len(q) == 1:
        c += 1
    else:
        a, b = int(q[1]), int(q[2])
        qs.append((a, b, c))
        c = 0
if c:
    qs.append((a, b, c))

R = [10**18]
L = [10**18]
ss = 0
for a, b, c in qs:
    xs = []
    if c:
        x = heappop(L)
        for _ in range(c):
            print(-x, ss)
        heappush(L, x)
    x = heappop(R)
    y = -heappop(L)
    if x < a:
        heappush(R, a)
        heappush(R, a)
        heappush(L, -x)
        heappush(L, -y)
        ss += x-a
    elif y > a:
        heappush(L, -a)
        heappush(L, -a)
        heappush(R, x)
        heappush(R, y)
        ss += y-a
    else:
        heappush(L, -a)
        heappush(R, a)
        heappush(L, -y)
        heappush(R, x)
    ss += b

Submission status: Wrong Answer

Solution code:
from heapq import *
Q, = map(int, input().split())
qs = []
c = 0
for i in range(Q):
    q = input().split()
    if len(q) == 1:
        c += 1
    else:
        a, b = int(q[1]), int(q[2])
        qs.append((a, b, c, i))
        c = 0
if c:
    qs.append((a, b, c, i))

R = [10**18]
L = [10**18]
ss = 0
for a, b, c, i in qs:
    xs = []
    if c:
        x = heappop(L)
        for _ in range(c):
            print(-x, ss)
        heappush(L, x)
    x = heappop(R)
    y = -heappop(L)
    if x < a:
        heappush(R, a)
        heappush(R, a)
        heappush(L, -x)
        heappush(L, -y)
        ss += x-a
    elif y > a:
        heappush(L, -a)
        heappush(L, -a)
        heappush(R, x)
        heappush(R, y)
        ss += y-a
    else:
        heappush(L, -a)
        heappush(R, a)
        heappush(L, -y)
        heappush(R, x)
    ss += b

Submission status: Wrong Answer

Solution code:
import bisect
import heapq
import sys


input = sys.stdin.readline
sys.setrecursionlimit(100000)


class v:
    def __init__(self, f):
        self.f = f
        self.v = None

    def __str__(self):
        return str(self.v)

    def ud(self, n):
        if self.v is None:
            self.v = n
        else:
            self.v = self.f(self.v, n)


def read_values():
    return map(int, input().split())


def read_list():
    return list(read_values())


def update(S, L, M):
    _, a, b = S

    M[0] += b

    if len(L[0]) == 0:
        L[0].append(-a)
        return

    if len(L[0]) == len(L[1]):
        r0 = -1 * L[0][0]
        r1 = L[1][0]
        if a < r0:
            M[1] += r0 - a
        elif r1 < a:
            M[1] += a - r1
    else:
        M[1] += abs(a + L[0][0])

    heapq.heappush(L[0], -a)
    if len(L[0]) - len(L[1]) >= 2:
        t = -1 * heapq.heappop(L[0])
        heapq.heappush(L[1], t)

    if -1 * L[0][0] > L[1][0]:
        t = -1 * heapq.heappop(L[0])
        t2 = heapq.heappushpop(L[1], t)
        heapq.heappush(L[0], t2)


def print_min(L, M):
    print(-1 * L[0][0], sum(M))


def main():
    Q = int(input())

    L = [[], []]
    M = [0, 0]
    for q in range(Q):
        S = read_list()
        if S[0] == 1:
            update(S, L, M)
        else:
            print_min(L, M)


if __name__ == "__main__":
    main()