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:
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))


"""
前回の最小区間を覚えておいて，差分で最小値を更新する
"""

def main():

    ##############################
    from bisect import bisect_left, bisect_right, insort_right
    class SquareSkipList:
        # SkipList の層数を 2 にした感じの何か
        # std::multiset の代用になる

        def __init__(self, values=None, sorted_=False, square=1000, seed=42):
            # values: 初期値のリスト
            # sorted_: 初期値がソート済みであるか
            # square: 最大データ数の平方根
            # seed: 乱数のシード
            ### pop関数は　for を回すので重め、使うなら square パラメータを大きめにするべき

            self.cnt=0#要素数を持っておくか
            inf = float("inf")
            self.square = square
            if values is None:
                self.rand_y = seed
                self.layer1 = [inf]
                self.layer0 = [[]]
            else:
                self.layer1 = layer1 = []
                self.layer0 = layer0 = []
                if not sorted_:
                    values.sort()
                y = seed
                l0 = []
                for v in values:
                    y ^= (y & 0x7ffff) << 13
                    y ^= y >> 17
                    y ^= (y & 0x7ffffff) << 5
                    if y % square == 0:
                        layer0.append(l0)
                        l0 = []
                        layer1.append(v)
                    else:
                        l0.append(v)
                layer1.append(inf)
                layer0.append(l0)
                self.rand_y = y

        def add(self, x):  # 要素の追加  # O(sqrt(n))
            # xorshift
            y = self.rand_y
            y ^= (y & 0x7ffff) << 13
            y ^= y >> 17
            y ^= (y & 0x7ffffff) << 5
            self.rand_y = y

            if y % self.square == 0:
                layer1, layer0 = self.layer1, self.layer0
                idx1 = bisect_right(layer1, x)
                layer1.insert(idx1, x)
                layer0_idx1 = layer0[idx1]
                idx0 = bisect_right(layer0_idx1, x)
                layer0.insert(idx1+1, layer0_idx1[idx0:])  # layer0 は dict で管理した方が良いかもしれない  # dict 微妙だった
                del layer0_idx1[idx0:]
            else:
                idx1 = bisect_right(self.layer1, x)
                insort_right(self.layer0[idx1], x)

            self.cnt+=1#追加

        def remove(self, x):  # 要素の削除  # O(sqrt(n))
            # x が存在しない場合、x 以上の最小の要素が削除される
            idx1 = bisect_left(self.layer1, x)
            layer0_idx1 = self.layer0[idx1]
            idx0 = bisect_left(layer0_idx1, x)
            if idx0 == len(layer0_idx1):
                del self.layer1[idx1]
                self.layer0[idx1] += self.layer0.pop(idx1+1)
            else:
                del layer0_idx1[idx0]

            self.cnt-=1

        def search_higher_equal(self, x):  # x 以上の最小の値を返す  O(log(n))
            idx1 = bisect_left(self.layer1, x)
            layer0_idx1 = self.layer0[idx1]
            idx0 = bisect_left(layer0_idx1, x)
            if idx0 == len(layer0_idx1):
                return self.layer1[idx1]
            return layer0_idx1[idx0]

        def search_higher(self, x):  # x を超える最小の値を返す  O(log(n))
            idx1 = bisect_right(self.layer1, x)
            layer0_idx1 = self.layer0[idx1]
            idx0 = bisect_right(layer0_idx1, x)
            if idx0 == len(layer0_idx1):
                return self.layer1[idx1]
            return layer0_idx1[idx0]

        def search_lower(self, x):  # x 未満の最大の値を返す  O(log(n))
            #x未満のものがない場合，infが返るかも！！！  
            idx1 = bisect_left(self.layer1, x)
            layer0_idx1 = self.layer0[idx1]
            idx0 = bisect_left(layer0_idx1, x)
            if idx0 == 0:  # layer0_idx1 が空の場合とすべて x 以上の場合
                return self.layer1[idx1-1]
            return layer0_idx1[idx0-1]

        def pop(self, idx):
            # 小さい方から idx 番目の要素を削除してその要素を返す（0-indexed）
            # O(sqrt(n))
            # for を回すので重め、使うなら square パラメータを大きめにするべき
            self.cnt-=1
            layer0 = self.layer0
            s = -1
            for i, l0 in enumerate(layer0):
                s += len(l0) + 1
                if s >= idx:
                    break
            if s==idx:
                layer0[i] += layer0.pop(i+1)
                return self.layer1.pop(i)
            else:
                return layer0[i].pop(idx-s)


        def all(self):
            print(self.layer1)
            print(self.layer0)

        def len(self):
            return self.cnt

    ##############################


    
    inf=10**9+1
    Q=I()
    ssl1 = SquareSkipList(square=2000)
    ssl1.add(-inf)
    ssl2 = SquareSkipList(square=2000)
    ssl2.add(inf)
    
    s1=0#和
    s2=0
    
    q,a,b=MI()
    mx=a#最小値を与えるx
    m=b#最小値
    
    
    for _ in range(Q-1):
        q=LI()
        if q[0]==1:
            a=q[1]
            m+=q[2]
            aa=ssl1.search_lower(inf)#下側の最大値
            bb=ssl2.search_higher(-inf)#上側の最小値
            l1=ssl1.len()
            l2=ssl2.len()
            # print(a,aa,mx,bb,l1,l2)
            
            #足す動作
            if a<aa:
                ssl1.add(a)
                s1+=a
            elif a>bb:
                ssl2.add(a)
                s2+=a
            else:
                if a>mx:
                    a,mx=mx,a
                if l2<=l1:
                    ssl2.add(mx)
                    s2+=mx
                    bb=mx
                    mx=a
                else:
                    ssl1.add(a)
                    aa=a
                    s1+=a
                    
            #帳尻合わせの移動
            if l1>l2:
                ssl2.add(mx)
                s2+=mx
                ssl1.remove(aa)
                s1-=aa
                mx=aa      
            else:
                if l2-l1>=2:
                    ssl1.add(mx)
                    s1+=mx
                    ssl2.remove(bb)
                    s2-=bb
                    mx=bb

        else:
            temp=m
            l1=ssl1.len() -1
            l2=ssl2.len() -1
            temp+=l1*mx-s1
            temp+=s2-l2*mx
            print(mx,temp)
                    
                
            
            
            
            
        
        
    

main()

Submission status: Wrong Answer

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

N = int(input())
L = []
R = []
Lsum = 0
Rsum = 0
const = 0


"""
[], []
[x], []
[x], [y]
[x, z], [y]
...
"""
for _ in range(N):
    f, *arg = map(int, input().split())
    if f == 1:
        a, b = arg
        const += b
        heapq.heappush(L, -a)
        Lsum += a
        while len(L) - len(R) > 1:
            x = -heapq.heappop(L)
            Lsum -= x
            heapq.heappush(R, x)
            Rsum += x
        while L and R and -L[0] > R[0]:
            x = -heapq.heappop(L)
            y = heapq.heappop(R)
            Lsum += y - x
            Rsum += x - y
            heapq.heappush(L, -y)
            heapq.heappush(R, y)
    else:
        xmed = -L[0]
        F = const
        F += Rsum - len(R) * xmed
        F += len(L) * xmed - Lsum
        print(xmed, F)

Submission status: Wrong Answer

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

N = int(input())
L = []
R = []
Lsum = 0
Rsum = 0
const = 0


"""
[], []
[x], []
[x], [y]
[x, z], [y]
...
"""
for _ in range(N):
    f, *arg = map(int, input().split())
    if f == 1:
        a, b = arg
        const += b
        heapq.heappush(R, a)
        Rsum += a
        if len(R) - len(L) >= 2:
            x = heapq.heappop(R)
            heapq.heappush(L, -x)
            Rsum -= x
            Lsum += x
    else:
        if len(L) == len(R):
            xmed = -L[0]
        else:
            xmed = R[0]
        F = const
        F += xmed * len(L) - Lsum
        F += Rsum - xmed * len(R)
        print(xmed, F)

Submission status: Wrong Answer

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

N = int(input())
L = []
R = []
Lsum = 0
Rsum = 0
const = 0


"""
[], []
[x], []
[x], [y]
[x, z], [y]
...
"""
for _ in range(N):
    f, *arg = map(int, input().split())
    if f == 1:
        a, b = arg
        const += b
        heapq.heappush(R, a)
        Rsum += a
        if len(R) - len(L) >= 2:
            x = heapq.heappop(R)
            heapq.heappush(L, -x)
            Rsum -= x
            Lsum += x
    else:
        if len(L) == len(R):
            xmed = -L[0]
        else:
            xmed = R[0]
        F = const
        F += Lsum - xmed * len(L)
        F += Rsum - xmed * len(R)
        print(xmed, F)

Submission status: Wrong Answer

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

N = int(input())
L = []
R = []
Lsum = 0
Rsum = 0
const = 0


"""
[], []
[x], []
[x], [y]
[x, z], [y]
...
"""
for _ in range(N):
    f, *arg = map(int, input().split())
    if f == 1:
        a, b = arg
        const += b
        heapq.heappush(L, -a)
        Lsum += a
        if len(L) - len(R) >= 2:
            x = -heapq.heappop(L)
            heapq.heappush(R, x)
            Lsum -= x
            Rsum += x
    else:
        xmed = -L[0]
        F = const
        F += Lsum - xmed * len(L)
        F += Rsum - xmed * len(R)
        print(xmed, F)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))


"""
前回の最小区間を覚えておいて，差分で最小値を更新する
"""

def main():

    ##############################
    from bisect import bisect_left, bisect_right, insort_right
    class SquareSkipList:
        # SkipList の層数を 2 にした感じの何か
        # std::multiset の代用になる

        def __init__(self, values=None, sorted_=False, square=1000, seed=42):
            # values: 初期値のリスト
            # sorted_: 初期値がソート済みであるか
            # square: 最大データ数の平方根
            # seed: 乱数のシード
            ### pop関数は　for を回すので重め、使うなら square パラメータを大きめにするべき

            self.cnt=0#要素数を持っておくか
            inf = float("inf")
            self.square = square
            if values is None:
                self.rand_y = seed
                self.layer1 = [inf]
                self.layer0 = [[]]
            else:
                self.layer1 = layer1 = []
                self.layer0 = layer0 = []
                if not sorted_:
                    values.sort()
                y = seed
                l0 = []
                for v in values:
                    y ^= (y & 0x7ffff) << 13
                    y ^= y >> 17
                    y ^= (y & 0x7ffffff) << 5
                    if y % square == 0:
                        layer0.append(l0)
                        l0 = []
                        layer1.append(v)
                    else:
                        l0.append(v)
                layer1.append(inf)
                layer0.append(l0)
                self.rand_y = y

        def add(self, x):  # 要素の追加  # O(sqrt(n))
            # xorshift
            y = self.rand_y
            y ^= (y & 0x7ffff) << 13
            y ^= y >> 17
            y ^= (y & 0x7ffffff) << 5
            self.rand_y = y

            if y % self.square == 0:
                layer1, layer0 = self.layer1, self.layer0
                idx1 = bisect_right(layer1, x)
                layer1.insert(idx1, x)
                layer0_idx1 = layer0[idx1]
                idx0 = bisect_right(layer0_idx1, x)
                layer0.insert(idx1+1, layer0_idx1[idx0:])  # layer0 は dict で管理した方が良いかもしれない  # dict 微妙だった
                del layer0_idx1[idx0:]
            else:
                idx1 = bisect_right(self.layer1, x)
                insort_right(self.layer0[idx1], x)

            self.cnt+=1#追加

        def remove(self, x):  # 要素の削除  # O(sqrt(n))
            # x が存在しない場合、x 以上の最小の要素が削除される
            idx1 = bisect_left(self.layer1, x)
            layer0_idx1 = self.layer0[idx1]
            idx0 = bisect_left(layer0_idx1, x)
            if idx0 == len(layer0_idx1):
                del self.layer1[idx1]
                self.layer0[idx1] += self.layer0.pop(idx1+1)
            else:
                del layer0_idx1[idx0]

            self.cnt-=1

        def search_higher_equal(self, x):  # x 以上の最小の値を返す  O(log(n))
            idx1 = bisect_left(self.layer1, x)
            layer0_idx1 = self.layer0[idx1]
            idx0 = bisect_left(layer0_idx1, x)
            if idx0 == len(layer0_idx1):
                return self.layer1[idx1]
            return layer0_idx1[idx0]

        def search_higher(self, x):  # x を超える最小の値を返す  O(log(n))
            idx1 = bisect_right(self.layer1, x)
            layer0_idx1 = self.layer0[idx1]
            idx0 = bisect_right(layer0_idx1, x)
            if idx0 == len(layer0_idx1):
                return self.layer1[idx1]
            return layer0_idx1[idx0]

        def search_lower(self, x):  # x 未満の最大の値を返す  O(log(n))
            #x未満のものがない場合，infが返るかも！！！  
            idx1 = bisect_left(self.layer1, x)
            layer0_idx1 = self.layer0[idx1]
            idx0 = bisect_left(layer0_idx1, x)
            if idx0 == 0:  # layer0_idx1 が空の場合とすべて x 以上の場合
                return self.layer1[idx1-1]
            return layer0_idx1[idx0-1]

        def pop(self, idx):
            # 小さい方から idx 番目の要素を削除してその要素を返す（0-indexed）
            # O(sqrt(n))
            # for を回すので重め、使うなら square パラメータを大きめにするべき
            self.cnt-=1
            layer0 = self.layer0
            s = -1
            for i, l0 in enumerate(layer0):
                s += len(l0) + 1
                if s >= idx:
                    break
            if s==idx:
                layer0[i] += layer0.pop(i+1)
                return self.layer1.pop(i)
            else:
                return layer0[i].pop(idx-s)


        def all(self):
            print(self.layer1)
            print(self.layer0)

        def len(self):
            return self.cnt

    ##############################


    
    mod=10**9+7
    Q=I()
    ssl = SquareSkipList(square=2000)
    ml=-10**9-1
    mr=10**9+1
    ssl.add(ml-1)
    ssl.add(mr+1)
    m=0#最小値
    
    for _ in range(Q):
        q=LI()
        if q[0]==1:
            ssl.add(q[1])
            m+=q[2]
            if ml<=q[1]<=mr:
                ml,mr=q[1],q[1]
            elif q[1]<ml:
                m+=ml-q[1]
                temp=ssl.search_lower(ml)
                ml,mr=temp,ml
            else:
                m+=q[1]-mr
                temp=ssl.search_higher(mr)
                ml,mr=ml,temp
                
        else:
            print(ml,m)
            
            
            
            
        
        
    

main()

Submission status: Wrong Answer

Solution code:
import heapq
import collections

cnt=1
cnts=collections.defaultdict(int)
median=0
left_cnt=0
right_cnt=0
left_sum=0
right_sum=0
left_pos=[]
right_pos=[]
b=0
q=int(input())
initial=list(map(int,input().split()))
median=initial[1]
b+=initial[2]
cnts[median]=1
for _ in range(q-1):
  query=list(map(int,input().split()))
  if query[0]==1:
    cnt+=1
    b+=query[2]
    if query[1]==median:
      cnts[median]+=1
    elif query[1]<median:
      left_cnt+=1
      left_sum+=query[1]
      cnts[query[1]]+=1
      heapq.heappush(left_pos,-query[1])
    elif query[1]>median:
      right_cnt+=1
      right_sum+=query[1]
      cnts[query[1]]+=1
      heapq.heappush(right_pos,query[1])
    if left_cnt<(cnt+1)//2<=left_cnt+cnts[median]:
      continue
    elif left_cnt>=(cnt+1)//2:
      heapq.heappush(right_pos,median)
      right_cnt+=cnts[median]
      right_sum+=median*cnts[median]
      median=-heapq.heappop(left_pos)
      left_cnt-=cnts[median]
      left_sum-=median*cnts[median]
    elif (cnt+1)//2>left_cnt+cnts[median]:
      heapq.heappush(left_pos,-median)
      left_cnt+=cnts[median]
      left_sum+=median*cnts[median]
      median=heapq.heappop(right_pos)
      right_cnt-=cnts[median]
      right_sum-=median*cnts[median]
  else:
    value=b
    if left_cnt!=0:
      value+=abs(left_sum-median*left_cnt)
    if right_cnt!=0:
      value+=abs(right_sum-median*right_cnt)
    print(median,value)

Submission status: Wrong Answer

Solution code:
import heapq
import collections

cnt=0
cnts=collections.defaultdict(int)
median=0
left_cnt=0
right_cnt=0
left_sum=0
right_sum=0
left_pos=[]
right_pos=[]
b=0
q=int(input())
initial=list(map(int,input().split()))
median=initial[1]
b+=initial[2]
cnts[median]=1
for _ in range(q-1):
  query=list(map(int,input().split()))
  if query[0]==1:
    cnt+=1
    b+=query[2]
    if query[1]==median:
      cnts[median]+=1
    elif query[1]<median:
      left_cnt+=1
      left_sum+=query[1]
      cnts[query[1]]+=1
      heapq.heappush(left_pos,-query[1])
    elif query[1]>median:
      right_cnt+=1
      right_sum+=query[1]
      cnts[query[1]]+=1
      heapq.heappush(right_pos,query[1])
    if left_cnt<(cnt+1)//2<=left_cnt+cnts[median]:
      continue
    elif left_cnt>=(cnt+1)//2:
      right_cnt+=cnts[median]
      right_sum+=median*cnts[median]
      median=-heapq.heappop(left_pos)
      left_cnt-=cnts[median]
      left_sum-=median*cnts[median]
    elif (cnt+1)//2>left_cnt+cnts[median]:
      left_cnt+=cnts[median]
      left_sum+=median*cnts[median]
      median=heapq.heappop(right_pos)
      right_cnt-=cnts[median]
      right_sum-=median*cnts[median]
  else:
    value=b
    if left_cnt!=0:
      value+=abs(left_sum-median*left_cnt)
    if right_cnt!=0:
      value+=abs(right_sum-median*right_cnt)
    print(median,value)

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush


class Medians:
    def __init__(self):
        self.left = []
        self.right = []
        self.size_l = 0
        self.size_r = 0
        self.sum_l = 0
        self.sum_r = 0

    def add(self, n):
        if self.size_l > self.size_r:
            if self.right:
                if -self.left[0] > n:
                    heappush(self.left, -n)
                    self.size_l += 1
                    self.sum_l += n
                else:
                    heappush(self.right, n)
                    self.size_r += 1
                    self.sum_r += n
            else:
                if -self.left[0] > n:
                    a = -heappop(self.left)
                    self.sum_l -= a
                    heappush(self.right, a)
                    self.sum_r += a
                    heappush(self.left, -n)
                    self.sum_l += n
                else:
                    heappush(self.right, n)
                    self.sum_r += n
                self.size_r += 1
        else:
            if self.left:
                if self.right and self.right[0] < n:
                    heappush(self.right, n)
                    self.size_r += 1
                    self.sum_r += n
                else:
                    heappush(self.left, -n)
                    self.size_l += 1
                    self.sum_l += n
            else:
                heappush(self.left, -n)
                self.size_l += 1
                self.sum_l += n
    
    def get(self):
        if self.size_l >= self.size_r:
            v = -heappop(self.left)
            heappush(self.left, -v)
        else:
            v = heappop(self.right)
            heappush(self.right, v)
        return v


def solve():
    Q = int(input())
    s = 0
    med = Medians()
    for _ in range(Q):
        q, *l, = map(int, input().split())
        if q == 1:
            a, b = l
            s += b
            med.add(a)
        else:
            m = med.get()
            d1 = med.size_l - med.size_r
            d2 = med.sum_r - med.sum_l
            print(m, m*d1+d2+s)


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush


class Medians:
    def __init__(self):
        self.left = []
        self.right = []
        self.size_l = 0
        self.size_r = 0
        self.sum_l = 0
        self.sum_r = 0

    def add(self, n):
        if self.size_l > self.size_r:
            if self.right:
                if -self.left[0] > n:
                    heappush(self.size_l, -n)
                    self.size_l += 1
                    self.sum_l += n
                else:
                    heappush(self.right, n)
                    self.size_r += 1
                    self.sum_r += n
            else:
                if -self.left[0] > n:
                    a = -heappop(self.left)
                    self.sum_l -= a
                    heappush(self.right, a)
                    self.sum_r += a
                    heappush(self.left, -n)
                    self.sum_l += n
                else:
                    heappush(self.right, n)
                    self.sum_r += n
                self.size_r += 1
        else:
            if self.left:
                if self.right and self.right[0] < n:
                    heappush(self.right, n)
                    self.size_r += 1
                    self.sum_r += n
                else:
                    heappush(self.left, -n)
                    self.size_l += 1
                    self.sum_l += n
            else:
                heappush(self.left, -n)
                self.size_l += 1
                self.sum_l += n
    
    def get(self):
        if self.size_l >= self.size_r:
            v = -heappop(self.left)
            heappush(self.left, -v)
        else:
            v = heappop(self.right)
            heappush(self.right, v)
        return v


def solve():
    Q = int(input())
    s = 0
    med = Medians()
    for _ in range(Q):
        q, *l, = map(int, input().split())
        if q == 1:
            a, b = l
            s += b
            med.add(a)
        else:
            m = med.get()
            d1 = med.size_l - med.size_r
            d2 = med.sum_r - med.sum_l
            print(m, m*d1+d2+s)

Submission status: Wrong Answer

Solution code:
import heapq


def main():
    Q = int(input())
    query_list = [list(map(int, input().split(' '))) for _ in range(Q)]
    # fの最小を与えるxの計算用
    que_left, que_right = [], []  # 最大ヒープと最小ヒープ
    # fの最小値の計算用
    before_a, sum_a_cost = None, 0
    sum_b = 0
    for query in query_list:
        if query[0] == 1:
            a, b = query[1], query[2]
            # xの更新
            if len(que_right) == 0 or a >= que_right[0]:
                heapq.heappush(que_right, a)
            else:
                # 最大ヒープにするため符号反転
                heapq.heappush(que_left, - a)
            # ヒープのサイズ差が1 or 2になるよう調整
            if len(que_right) - len(que_left) == 0:
                heapq.heappush(que_right, - heapq.heappop(que_left))
            elif len(que_right) - len(que_left) == 3:
                heapq.heappush(que_left, - heapq.heappop(que_right))
            # fの更新
            if before_a is None:
                before_a = a
            else:
                sum_a_cost += abs(a - before_a)
                before_a = None
            sum_b += b
        else:
            x = que_right[0]
            f = sum_a_cost + sum_b
            print('{} {}'.format(x, f))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from heapq import heappush, heappop, heapify

INF = float("inf")


def main():
    Q = int(input())
    bottom = [INF]                 # 最大ヒープ
    top = [INF]               # 最小ヒープ
    bottom_total = 0
    top_total = 0

    btot = 0
    for i in range(Q):
        q = input()
        if q[0] == "1":
            _, a, b = map(int, q.split())
            # print("a, b", a, b)
            btot += b
            if -bottom[0] < a:
                # topに加えるか、bottomに加えるか
                if len(top) < len(bottom):
                    heappush(top, a)
                    top_total += a
                else:
                    heappush(bottom, -a)
                    bottom_total += a
            else:
                # a <= -bottom[0] ならbottomに加える
                if len(top) < len(bottom):
                    heappush(top, -bottom[0])
                    top_total += -bottom[0]
                    bottom_total -= -bottom[0]
                    heappop(bottom)

                    heappush(bottom, -a)
                    bottom_total += a
                else:
                    heappush(bottom, -a)
                    bottom_total += a
        else:
            tot = btot
            tot += (len(bottom)*(-bottom[0]) - bottom_total)
            tot += (top_total - len(top)*(-bottom[0]))
            print(-bottom[0], tot)
            # print(bottom, top)

    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def examA():
    N = I()
    ans = 0
    print(ans)
    return

def examB():
    ans = 0
    print(ans)
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    # 0は絶対に入れない!!
    class Bit():
        def __init__(self, n):
            self.size = n
            self.tree = [0] * (n + 1)
            return

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

        def add(self, i, x=1):
            # i==0 はだめ => 全部+1するとか
            i += 1
            while i <= self.size:
                self.tree[i] += x
                i += i & -i
            return

        def search(self, x):
            # 二分探索。和がx以上となる最小のインデックス(>= 1)を返す
            # maspyさんの参考　よくわかってない
            i = 0
            s = 0
            step = 1 << ((self.size).bit_length() - 1)
            while step:
                if i + step <= self.size and s + self.tree[i + step] < x:
                    i += step
                    s += self.tree[i]
                step >>= 1
            return i

        def debug(self, k):
            return [self.sum(i) for i in range(k)]
    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)
    n = I()
    Q = [LI()for _ in range(n)]
    A = []
    for q in Q:
        if q[0]==2:
            continue
        a, b = q[1:]
        A.append(a)
    memo,N = compress(A)
    #print(memo)
    #print(N)
    bit = Bit(N+1)
    bit_a = Bit(N+1)
    location = defaultdict(int)
    num = 0
    B = 0
    ans = []
    for q in Q:
        if q[0]==2:
            x = bit.search((num+1)//2)
            #print(x,bit_a.sum(x),location[x])
            rep = location[x]*bit.sum(x)-bit_a.sum(x)+B
            if num%2==0:
                rep += bit_a.sum(N) - bit_a.sum(x) - location[x+1] * bit.sum(x)
                rep += (location[x+1]-location[x])
            else:
                rep += bit_a.sum(N) - bit_a.sum(x) - location[x]*(bit.sum(N) - bit.sum(x))
            ans.append((location[x],rep))
            continue
        a, b = q[1:]
        B += b
        bit.add(memo[a])
        bit_a.add(memo[a],a)
        location[memo[a]] = a
        num += 1

    for v in ans:
        print(" ".join(map(str,v)))
    return

def test():
    i = I()
    li = LI()
    lsi = LSI()
    si = LS()
    print(i)
    print(li)
    print(lsi)
    print(si)
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(readline())
def LI(): return list(map(int,readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examF()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""