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:
# -*- coding: utf-8 -*-
"""
Created on Sat May 25 22:55:33 2019

@author: Yamazaki Kenichi
"""
import heapq

Q = int(input())
Q2 = [input() for i in range(Q)]

ans = []
Al,Ar = [-(-(10**9+1))],[10**9+1]
B = 0
flg = False
for q in Q2:
    if q[0] == '1':
#        input
        d, a, b = map(int,q.split())
        if not flg:
            heapq.heappush(Al,-a)
            B += b
            flg = True
            continue

        if len(Al) == len(Ar):
            if a < (-Al[0]):
                heapq.heappush(Al,-a)
                C = abs(a - (-Al[0]))
            else:
                heapq.heappush(Al,-(heapq.heappop(Ar)))
                heapq.heappush(Ar,a)
                C = abs(a - (-Al[0]))
        else:
            if a < (-Al[0]):
                heapq.heappush(Ar,-(heapq.heappop(Al)))
                heapq.heappush(Al,-a)
                C = abs(a - (Ar[0]))
            else:
                heapq.heappush(Ar,a)
                C = abs(a - (-Al[0]))
        B += (b + C)
#        print(Al,Ar,a,B,b)
    else:
#        output
        ans.append([(-Al[0]),B])
        
for c in ans:
    print(' '.join(map(str,c)))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat May 25 22:55:33 2019

@author: Yamazaki Kenichi
"""
import heapq

Q = int(input())
Q2 = [input() for i in range(Q)]

ans = []
Al,Ar = [-(-(10**9+1))],[10**9+1]
B = 0
flg = False
for q in Q2:
    if q[0] == '1':
#        input
        d, a, b = map(int,q.split())
        if not flg:
            heapq.heappush(Al,-a)
            flg = True
            continue

        if len(Al) == len(Ar):
            if a < (-Al[0]):
                heapq.heappush(Al,-a)
                C = abs(a - (-Al[0]))
            else:
                heapq.heappush(Al,-(heapq.heappop(Ar)))
                heapq.heappush(Ar,a)
                C = abs(a - (-Al[0]))
        else:
            if a < (-Al[0]):
                heapq.heappush(Ar,-(heapq.heappop(Al)))
                heapq.heappush(Al,-a)
                C = abs(a - (Ar[0]))
            else:
                heapq.heappush(Ar,a)
                C = abs(a - (-Al[0]))
        B += (b + C)
#        print(Al,Ar,a)
    else:
#        output
        ans.append([(-Al[0]),B])
        
for c in ans:
    print(' '.join(map(str,c)))

Submission status: Wrong Answer

Solution code:
from heapq import heapify, heappush, heappop
N = int(input())
s = list(map(int,input().split()))
a, b = s[1], s[2]
L = []
R = []
heappush(L,-a)
heappush(R,a)

bsum = b
t = 0
for k in range(N-1):
    s = input()
    if s[0] == "1":
        s = list(map(int,s.split()))
        a = s[1]
        b = s[2]
        bsum += b
        if -L[0] <= a <= R[0]:
            pass
        else:
            t = min(abs(a+L[0]),abs(a-R[0]))
        heappush(L,-a)
        heappush(R,a)
        if -L[0] > R[0]:
            c = heappop(L)
            d = heappop(R)
            heappush(R,-c)
            heappush(L,-d)
    else:
        print(-L[0], t+bsum)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
N = int(input())
s = list(map(int,input().split()))
a, b = s[1], s[2]
L = []
R = []
heappush(L,-a)
heappush(R,a)

bsum = b
t = 0
for k in range(N-1):
    s = input()
    if s[0] == "1":
        s = list(map(int,s.split()))
        a = s[1]
        b = s[2]
        bsum += b
        if -L[0] <= a <= R[0]:
            pass
        else:
            t = min(abs(a+L[0]),abs(a-R[0]))
        heappush(L,-a)
        heappush(R,a)
        if -L[0] > R[0]:
            c = heappop(L)
            d = heappop(R)
            heappush(R,-c)
            heappush(L,d)
    else:
        print(-L[0], t+bsum)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
N = int(input())
s = list(map(int,input().split()))
a, b = s[1], s[2]
L = []
R = []
heappush(L,-a)
heappush(R,a)

bsum = b
t = 0
for k in range(N-1):
    s = input()
    if s[0] == "1":
        s = list(map(int,s.split()))
        a = s[1]
        b = s[2]
        bsum += b
        t = min(abs(a+L[0]),abs(a-R[0]))
        heappush(L,-a)
        heappush(R,a)
        if -L[0] > R[0]:
            c = heappop(L)
            d = heappop(R)
            heappush(R,-c)
            heappush(L,d)
    else:
        print(-L[0], t+bsum)

Submission status: Wrong Answer

Solution code:
from collections import deque
Q = int(input())
S = []
L = []
CO = 0
MIN = 0
for i in range(Q):
    LI = list(map(int,input().split()))
    if LI[0] == 1:
        CO += LI[2]
        if len(S) == len(L):
            if len(S) == 0:
                S.append(LI[1])
            elif LI[1] <= L[0]:
                if LI[1] < S[-1]:
                    MIN += S[-1] - LI[1]
                S.append(LI[1])
                S.sort()
            else:
                MIN += LI[1] - L[0]
                S.append(L[0])
                L.pop(0)
                L.append(LI[1])
                L.sort()
        else:
            if LI[1] < S[-1]:
                MIN += S[-1] - LI[1]
                L.insert(0,S[-1])
                S.pop(-1)
                S.append(LI[1])
                S.sort()
            else:
                if len(L) == 0:
                    MIN += LI[1] - S[-1]
                    L.append(LI[1])
                    L.sort()
                else:
                    MIN += L[0] - S[-1]
                    L.append(LI[1])
                    L.sort()
    else:
        print(str(S[-1])+' '+str(MIN+CO))

Submission status: Wrong Answer

Solution code:
import sys
import bisect
Q = int(input().rstrip())
queries = []
for _ in range(Q):
    queries.append(list(map(int, input().rstrip().split(" "))))

bias = 0
vs = []

import time
s1 = time.time()
e1 = None
e2 = None



qi = 0
for query in queries:
    if len(query) == 3:
        qi += 1
        _, v, b = query
        bisect.insort_left(vs, v)
        bias += b
        if qi == 1:
            abs_sum = 0
            med = v
            medp = 0
        else:
            if qi % 2 == 0:
                if v <= med:
                    # * ^ + -> v * ^ + -> v ^ + +
                    #       -> * v ^ + -> * ^ + +
                    # medp -> same
                    # med -> new
                    # abs_sum -> += abs(new_med - med) + abs(new_med - v) = (med - new_med) + (new_med - v) = med - v
                    abs_sum += med - v
                    med = vs[medp]
                else:
                    # * ^ + -> * ^ + v
                    #       -> * ^ v +
                    # medp -> same
                    # med -> same
                    # abs_sum -> += abs(med - v) = v - med
                    abs_sum += v - med
            else:
                if v <= med:
                    # * ^ + + -> v * ^ + +
                    #         -> * v ^ + +
                    # ^ + -> v ^ +
                    # medp -> += 1
                    # med -> same
                    # abs_sum -> += abs(med - v) = med - v
                    abs_sum += med - v
                    medp += 1
                else:
                    # * ^ + + -> * ^ + + v -> * * ^ + v
                    #         -> * ^ v + + -> * * ^ + +
                    # medp -> += 1
                    # med -> new
                    # abs_sum -> += abs(med - v) = v - med
                    medp += 1
                    abs_sum += v - med
    else:
        if e1 is None:
            e1 = time.time()
        #i = (len(vs)+1) // 2 - 1
        #p = vs[i]
        #l = p*i - sum(vs[:i])
        #r = sum(vs[i:]) - (p*(len(vs)-i))
        #s = l + r
        #print(p, s+bias)

        print(med, abs_sum+bias, vs, medp, file=sys.stderr)
        print(med, abs_sum+bias)

e2 = time.time()
print(e1-s1, e2-e1, file=sys.stderr)

Submission status: Wrong Answer

Solution code:
from heapq import*

def _heappop_max(heap):
    """Maxheap version of a heappop."""
    lastelt = heap.pop()    # raises appropriate IndexError if heap is empty
    if heap:
        returnitem = heap[0]
        heap[0] = lastelt
        _siftup_max(heap, 0)
        return returnitem
    return lastelt

def _heapreplace_max(heap, item):
    """Maxheap version of a heappop followed by a heappush."""
    returnitem = heap[0]    # raises appropriate IndexError if heap is empty
    heap[0] = item
    _siftup_max(heap, 0)
    return returnitem

def _heapify_max(x):
    """Transform list into a maxheap, in-place, in O(len(x)) time."""
    n = len(x)
    for i in reversed(range(n//2)):
        _siftup_max(x, i)
def _siftdown(heap, startpos, pos):
    newitem = heap[pos]
    while pos > startpos:
        parentpos = (pos - 1) >> 1
        parent = heap[parentpos]
        if newitem < parent:
            heap[pos] = parent
            pos = parentpos
            continue
        break
    heap[pos] = newitem

def _siftup(heap, pos):
    endpos = len(heap)
    startpos = pos
    newitem = heap[pos]
    childpos = 2*pos + 1    # leftmost child position
    while childpos < endpos:
        rightpos = childpos + 1
        if rightpos < endpos and not heap[childpos] < heap[rightpos]:
            childpos = rightpos
        heap[pos] = heap[childpos]
        pos = childpos
        childpos = 2*pos + 1
    heap[pos] = newitem
    _siftdown(heap, startpos, pos)

def _siftdown_max(heap, startpos, pos):
    'Maxheap variant of _siftdown'
    newitem = heap[pos]
    while pos > startpos:
        parentpos = (pos - 1) >> 1
        parent = heap[parentpos]
        if parent < newitem:
            heap[pos] = parent
            pos = parentpos
            continue
        break
    heap[pos] = newitem

def _siftup_max(heap, pos):
    'Maxheap variant of _siftup'
    endpos = len(heap)
    startpos = pos
    newitem = heap[pos]
    childpos = 2*pos + 1    # leftmost child position
    while childpos < endpos:
        rightpos = childpos + 1
        if rightpos < endpos and not heap[rightpos] < heap[childpos]:
            childpos = rightpos
        heap[pos] = heap[childpos]
        pos = childpos
        childpos = 2*pos + 1
    heap[pos] = newitem
    _siftdown_max(heap, startpos, pos)

def merge(*iterables, key=None, reverse=False):
    h = []
    h_append = h.append

    if reverse:
        _heapify = _heapify_max
        _heappop = _heappop_max
        _heapreplace = _heapreplace_max
        direction = -1
    else:
        _heapify = heapify
        _heappop = heappop
        _heapreplace = heapreplace
        direction = 1

    if key is None:
        for order, it in enumerate(map(iter, iterables)):
            try:
                next = it.__next__
                h_append([next(), order * direction, next])
            except StopIteration:
                pass
        _heapify(h)
        while len(h) > 1:
            try:
                while True:
                    value, order, next = s = h[0]
                    yield value
                    s[0] = next()           # raises StopIteration when exhausted
                    _heapreplace(h, s)      # restore heap condition
            except StopIteration:
                _heappop(h)                 # remove empty iterator
        if h:
            # fast case when only a single iterator remains
            value, order, next = h[0]
            yield value
            yield from next.__self__
        return

    for order, it in enumerate(map(iter, iterables)):
        try:
            next = it.__next__
            value = next()
            h_append([key(value), order * direction, value, next])
        except StopIteration:
            pass
    _heapify(h)
    while len(h) > 1:
        try:
            while True:
                key_value, order, value, next = s = h[0]
                yield value
                value = next()
                s[0] = key(value)
                s[2] = value
                _heapreplace(h, s)
        except StopIteration:
            _heappop(h)
    if h:
        key_value, order, value, next = h[0]
        yield value
        yield from next.__self__
        
#アプデしたらここから下でおｋになるはず

from heapq import*
n=int(input())
x=[]
y=[]
z=0
for i in range(n):
    I=input()
    if I[0]==str(1):
        a,b,c=map(int,I.split())
        z+=c
        if len(x)==len(y):
            heappush(y,b)
            heapify(y)
            heappush(x,heappop(y))
        else:
            heappush(x,b)
            _heapify_max(x)
            heappush(y,_heappop_max(x))
        heapify(x)
    else:
        tmp=x[-1]
        print(tmp,len(x)*tmp-sum(x or [0])+sum(y or [0])-len(y)*tmp+z)

Submission status: Wrong Answer

Solution code:
import heapq

Q = int(input())

A = []
A_size = 0
A_med_min = 0
A_med_max = 0
MIN_SCORE = 0
for i in range(Q):
    q = input()
    if q[0]=="1": #更新
        dummy,a,b = map(int,q.split())
        #常にソート済リストを作れる
        heapq.heappush(A,a)
        #bisect.insort_left(A,a)
        A_size+=1
        #中央値との差分からA群からなる最小値を更新
        sabun = 0
        if a<A_med_min:
            sabun = A_med_min - a
        elif a>A_med_max:
            sabun = a - A_med_max
        MIN_SCORE += sabun + b if A_size>1 else b
        #中央値の更新
        if A_size%2==0:
            A_med_min = A[(A_size//2)-1]
            A_med_max = A[A_size//2]
        else:
            A_med_min = A[A_size//2]
            A_med_max = A[A_size//2]
    elif q[0]=="2":
        print(A_med_min,MIN_SCORE)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import bisect
from collections import deque


def main():
  OFFSET = 10**9
  N = int(input())
  b = 0
  An = []
  Rn = deque()
  al,ar = [0,0]
  for _ in range(N):
    l = list(map(int, input().split()))
    if l[0] == 1:
      b += l[2]
      idx = bisect.bisect_left(An,l[1]+OFFSET)
      if len(An) > 0:
        if idx <= (len(An)-1)//2:
          ar += An[(len(An)-1)//2]
        else:
          al += An[(len(An)-1)//2]
      An.insert(idx, l[1]+OFFSET)

    else:
      ret = b
      c = (len(An)-1)//2
      ret += An[c]*c - al
      ret += ar - An[c]*(len(An)-c-1)
      Rn.append([An[c]-OFFSET,ret])

  return Rn


if __name__ == "__main__":
  for a,b in main():
    print(a,b)

Submission status: Wrong Answer

Solution code:
from heapq import*
from numpy import*
n=int(input())
l=[]
x=0
for i in range(n):
    I=input()
    if I[0]==str(1):
        a,b,c=map(int,I.split())
        heappush(l,b)
        x+=c
    else:
#        print(l,c)
        if len(l)%2:
            tmp=int(median(l))
            print(tmp,sum(abs(array(l)-tmp))+x)
        else:
            tmp=l[len(l)//2-1]
            print(tmp,sum(abs(array(l)-tmp))+x)

Submission status: Wrong Answer

Solution code:
import heapq


def main():
    lower = []
    upper = []
    x = 0
    fx = 0

    q = int(input())

    query = input().strip().split()
    _, x, fx = map(int, query)

    for _ in range(1, q):
        query = input().strip().split()
        if query[0] == '1':
            _, a, b = map(int, query)
            if a < x:
                heapq.heappush(lower, +a)
                while len(lower) + 1 > len(upper):
                    heapq.heappush(upper, -x)
                    x0 = x
                    x = +heapq.heappop(lower)
                    fx += abs(x - x0)
            else:
                heapq.heappush(upper, -a)
                while len(lower) + 1 < len(upper):
                    heapq.heappush(lower, +x)
                    x = -heapq.heappop(upper)
            fx += abs(x - a) + b
        if query[0] == '2':
            print(x, fx)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import heapq


def main():
    lower = []
    upper = []
    x = 0
    fx = 0

    q = int(input())

    query = input().strip().split()
    _, x, fx = map(int, query)

    for _ in range(1, q):
        query = input().strip().split()
        if query[0] == '1':
            _, a, b = map(int, query)
            if a < x:
                heapq.heappush(lower, +a)
                while len(lower) + 1 > len(upper):
                    heapq.heappush(upper, -x)
                    x = +heapq.heappop(lower)
            else:
                heapq.heappush(upper, -a)
                while len(lower) + 1 < len(upper):
                    heapq.heappush(lower, +x)
                    x = -heapq.heappop(upper)
            fx += abs(x - a) + b
        if query[0] == '2':
            print(x, fx)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
from collections import defaultdict
import math
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
q = int(input())
tei = 0
x = []
nnn = []
import bisect
def press(a):
    n = len(a)
    b = set(a)
    b = list(b)
    b.sort()
    c = [0] * n
    for i in range(n):
        c[i] = bisect.bisect_left(b,a[i])
    return b,c
for i in range(q):
        nnn.append(input().rstrip())
for i in range(q):
        nn = nnn[i]
        if int(nn[0]) == 1:
                _,a,b = map(int,nn.split())
                x.append(a)
x.sort()
mo,za = press(x)
mo = x
d = dict()
for i in range(len(x)):
        d[mo[i]] = za[i]+1
dg = dict()
for i in range(len(x)):
        dg[za[i]+1] = mo[i]
class BIT:
    def __init__(self, node_size):
        self._node = node_size+1
        self.bit = [0]*self._node

    def add(self, index, add_val):
        while index < self._node:
            self.bit[index] += add_val
            index += index & -index

    def sum(self, index):
        res = 0
        while index > 0:
            res += self.bit[index]
            index -= index & -index
        return res
bit1 = BIT(len(x))
bit2 = BIT(len(x))
now = -1
def cheak(x,y):
  o = bit2.sum(x)
  if y >= o:
    return 0
  else:
    return 1
for i in range(q):
        nn = nnn[i]
        if int(nn[0]) == 1:
                _,a,b = map(int,nn.split())
                tei += b
                bit1.add(d[a],a)
                bit2.add(d[a],1)
                now += 1
        else:
                dai = len(x)
                syo = 1
                while True:
                        if dai-syo <= 1:
                                if cheak(syo,now//2) == 1:
                                        dai = syo
                                break
                        kon = (dai+syo)//2
                        c = cheak(kon,now//2)
                        if c == 1:
                                dai = kon
                        else:
                                syo = kon
                ans = 0
                ans -= bit1.sum(dai) - dg[dai]*bit2.sum(dai)
                ans += bit1.sum(len(x)) -bit1.sum(dai) -(bit2.sum(len(x)-bit2.sum(dai))*dg[dai])
                print(dg[dai],tei+ans)

Submission status: Wrong Answer

Solution code:
import sys

def f(A, B):
    n = len(A)
    if n == 0:
        return (0, B)
    if n == 1:
        return (A[0] , B)

    A.sort()
    diff = 0
    for i in range(n-1):
        diff += A[i+1] - A[i]
    x = A[-2]
    return (x, diff + B)


def main():
    input = sys.stdin.readline
    INF = 10**9 + 1
    Q = int(input())
    A = []
    B = 0
    max_a =  -INF
    min_a = INF
    x = -INF
    for _ in range(Q):
        q = list(map(int, input().split()))
        if q[0] == 1:
            a, b = q[1], q[2]
            B += b
            if a > max_a:
                x = max_a
                max_a = a

            min_a = min(min_a, a)

        else:
            print('{} {}'.format(max(x, min_a), B + max_a - min_a))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
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 = max(keisuu_lower_max, a)
    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 = min(keisuu_upper_min, a)
    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)

            elif keisuu_lower_count > keisuu_upper_count:
                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()

Submission status: Wrong Answer

Solution code:
import sys

def f(A, B):
    n = len(A)
    if n == 0:
        return (0, B)
    if n == 1:
        return (A[0] , B)

    A.sort()
    diff = 0
    for i in range(n-1):
        diff += A[i+1] - A[i]
    x = A[-2]
    return (x, diff + B)


def main():
    input = sys.stdin.readline
    Q = int(input())
    A = []
    B = 0
    for _ in range(Q):
        q = list(map(int, input().split()))
        if q[0] == 1:
            a, b = q[1], q[2]
            A.append(a)
            B += b
        else:
            print(*f(A, B))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
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 = max(keisuu_lower_max, a)
    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 = min(keisuu_upper_min, a)
    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):

    #d = heapq.heappop(keisuu_upper)
    e = -heapq.heappop(keisuu_lower)
    #heapq.heappush(keisuu_lower, -d)
    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)
    #e = -heapq.heappop(keisuu_lower)
    heapq.heappush(keisuu_lower, -d)
    #heapq.heappush(keisuu_upper, e)
    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)

            elif keisuu_lower_count > keisuu_upper_count:
                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)

            #print(keisuu_lower, keisuu_lower_max, keisuu_lower_sum, keisuu_lower_count,
            #                              keisuu_upper, keisuu_upper_min, keisuu_upper_sum, keisuu_upper_count)

main()