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

Q = int(input())
ans = []
b = 0
ab = []
def ab_sum(a, center):
    length = len(a)
    a.sort()
    if length == 2:
        center = 0
        return a[1] - a[0]
    elif length == 1:
        center = 0
        return 0
    elif length % 2 == 0:
        center = int((length-2)/2)
        return sum(a[1+center:]) - sum(a[:center])
    else:
        center = int(length // 2)
        return sum(a[center+1:]) - sum(a[:center])




for i in range(Q):
    a = list(map(int, input().split()))
    
    if len(a) == 1:
        
        ab.sort()
        center = 0
        ss = ab_sum(ab, center)
        print(ab[center] ,b + ss)
    else:
        b += a[2]
        ab.append(a[1])

Submission status: Wrong Answer

Solution code:
import bisect as bi
Q = 4
a_li = []
scalar = 0
length = 0
for i in range(Q):
    query = list(map(int,input().split()))    
    if query[0] == 1:
        bi.insort_left(a_li,query[1])
        scalar += query[2]
        length += 1
#         print(a_li)
    
    if query[0] == 2:
#         print(a_li)
        if length % 2 ==0:
            middle = int(length/2)
            anslin = - sum(a_li[:middle]) + sum(a_li[middle:])
            position = a_li[middle-1]
        else:
            middle = int((length-1)/2) #5 -> 2, 7-> 3
            anslin = - sum(a_li[:middle]) + sum(a_li[middle+1:])
            position = a_li[middle]
        ans = anslin + scalar
        print(position,ans)

Submission status: Wrong Answer

Solution code:
Q = int(input())
INF = 10 ** 9

queries = []
for _ in range(Q):
    queries.append(input())

from bisect import insort_left
from heapq import heappush, heappop

absum, bsum, c = 0, 0, 0
# left, right = -INF, INF
lefts, rights = [INF], [INF]
for query in queries:
    if query[0] == '2':
        # absum = sum(abs(a - ary[(c-1)//2]) for a in ary)
        # absum = asum - (c//2 + 1) * ary[(c-1)//2]
        # print(' '.join(map(str, (ary[(c-1)//2], absum + bsum))))
        # print(str(ary[(c-1)//2]) + ' ' + str(absum + bsum))
        print(-lefts[0], absum + bsum)
    else:
        _, a, b = map(int, query.split())
        if -lefts[0] <= a <= rights[0]:
            heappop(lefts)
            heappop(rights)
            heappush(lefts, -a)
            heappush(rights, a)
            absum += 0
        elif a < -lefts[0]:
            heappush(lefts, -a)
            x = -heappop(lefts)
            heappush(rights, x)
            absum += x - a
        else:
            heappush(rights, a)
            x = heappop(rights)
            heappush(lefts, -x)
            absum += a - x
        # left, right = ary[(c-1)//2], ary[c - (c-1)//2 - 1]        
        bsum += b
        c += 1

Submission status: Wrong Answer

Solution code:
import heapq
Q = int(input())

lefts, rights = [], []  # lefts:aの左半分（中央値含む, 降順） rights:aの右半分
diff = 0    # aの中央値からの差の絶対値
bsum = 0    # bの累計

for i in range(Q):
    query = list(map(int, input().split()))
    if query[0] == 1:   # 更新クエリ(1)の処理
        a = query[1]
        bsum += query[2]
        if len(lefts) == 0:
            heapq.heappush(lefts, -a)   # 最初はleftsに降順に格納（中央値）
        else:
            if a <= -lefts[0]:
                heapq.heappush(lefts, -a)   # 現在の中央値以下ならleftsに降順にpush
                diff -= a + lefts[0]        # 現在の中央値(-lefts[0])との差
            else:
                heapq.heappush(rights, a)   # 現在の中央値より大きいならrightsにpush
                diff += a + lefts[0]        # 現在の中央値(-lefts[0])との差
        # lefts が rights と同数か1多い状態をキープするため、ずれたら移動
        if len(lefts) < len(rights):    # 偶数状態からrightsにpushされ、rightsが1件多い、奇数状態
            med = heapq.heappop(rights) # 新しい中央値をrightsからpop
            diff += med + lefts[0]      # 奇数なので、rightsの1件以外相殺されるから、新中央値と旧中央値の差を加える
            heapq.heappush(lefts, -med) # 新しい中央値をleftsにpush
        elif len(lefts) > len(rights) + 1:  # 奇数状態からleftsにpushされ、leftsが2件多い、偶数状態
            med = -heapq.heappop(lefts)      # これまでの中央値をleftsからpop（-1倍）
            # 偶数なので、中央値がずれても相殺されるから、diffは修正不要
            heapq.heappush(rights, med)     # これまでの中央値をrightsにpush
    else:   # 求値クエリ(2)の処理
        x = -lefts[0]       # 中央値がx
        fx = diff + bsum    # 中央値からの差の絶対値(diff)とbの累計(bsum)の和
        print(x, fx)

Submission status: Wrong Answer

Solution code:
import heapq
Q = int(input())

lefts, rights = [], []  # lefts:aの左半分（中央値含む, 降順） rights:aの右半分
diff = 0    # aの中央値からの差の絶対値
bsum = 0    # bの累計

for i in range(Q):
    query = list(map(int, input().split()))
    if query[0] == 1:   # 更新クエリ(1)の処理
        a = query[1]
        bsum += query[2]
        if len(lefts) == 0:
            heapq.heappush(lefts, -a)   # 最初はleftsに降順に格納（中央値）
        else:
            if a <= -lefts[0]:
                heapq.heappush(lefts, -a)   # 現在の中央値以下ならleftsに降順にpush
                diff -= a + lefts[0]        # 現在の中央値(-lefts[0])との差
            else:
                heapq.heappush(rights, a)   # 現在の中央値より大きいならrightsにpush
                diff += a + lefts[0]        # 現在の中央値(-lefts[0])との差
        # lefts が rights と同数か1多い状態をキープするため、ずれたら移動
        if len(lefts) < len(rights):    # 偶数状態からrightsにpushされ、rightsが1件多い、奇数状態
            med = heapq.heappop(rights) # 新しい中央値をrightsからpop
            diff += med + lefts[0]      # 奇数なので、rightsの1件以外相殺されるから、新中央値と旧中央値の差を加える
            heapq.heappush(lefts, -med) # 新しい中央値をleftsにpush
        elif len(lefts) > len(rights) + 1:  # 奇数状態からleftsにpushされ、leftsが2件多い、偶数状態
            med = heapq.heappop(lefts)      # これまでの中央値をleftsからpop
            # 偶数なので、中央値がずれても相殺されるから、diffは修正不要
            heapq.heappush(rights, med)     # これまでの中央値をrightsにpush
    else:   # 求値クエリ(2)の処理
        x = -lefts[0]       # 中央値がx
        fx = diff + bsum    # 中央値からの差の絶対値(diff)とbの累計(bsum)の和
        print(x, fx)

Submission status: Wrong Answer

Solution code:
Q = int(input())
INF = 10 ** 9

queries = []
for _ in range(Q):
    queries.append(input())

from bisect import insort_left
from heapq import heappush, heappop

absum, bsum, c = 0, 0, 0
# left, right = -INF, INF
lefts, rights = [INF], [INF]
for query in queries:
    if query[0] == '2':
        # absum = sum(abs(a - ary[(c-1)//2]) for a in ary)
        # absum = asum - (c//2 + 1) * ary[(c-1)//2]
        # print(' '.join(map(str, (ary[(c-1)//2], absum + bsum))))
        # print(str(ary[(c-1)//2]) + ' ' + str(absum + bsum))
        print(-lefts[0], absum + bsum)
    else:
        _, a, b = map(int, query.split())
        if -lefts[0] <= a <= rights[0]:
            heappush(lefts, -a)
            heappush(rights, a)
            absum += 0
        elif a < -lefts[0]:
            heappush(lefts, -a)
            x = -heappop(lefts)
            heappush(rights, x)
            absum += x - a
        else:
            heappush(rights, a)
            x = heappop(rights)
            heappush(lefts, -x)
            absum += a - x
        # left, right = ary[(c-1)//2], ary[c - (c-1)//2 - 1]        
        bsum += b
        c += 1

Submission status: Wrong Answer

Solution code:
class func:
    def __init__(self, a, b):
        self.A = [a]
        self.is_odd = True
        self.middle = 0
        self.min_fx = b
        self.len = 1

    def ins(self, a, b):
        tmp = self.A[self.middle]
        if not self.is_odd:
            self.middle += 1
        ok = False
        for i in range(self.len):
            if self.A[i] >= a:
                self.A.insert(i, a)
                ok = True
                break
        if not ok:
            self.A.append(a)
        self.is_odd = not(self.is_odd)
        self.len += 1
        self.min_fx += abs(self.A[self.middle] - a) + abs(self.A[self.middle] - tmp) + b

Q = int(input())
tmp, a, b = map(int, input().split())

f = func(a, b)

ans = []

for i in range(Q-1):
    query = input()
    if query[0] == "1":
        tmp, a, b = map(int, query.split())
        f.ins(a, b)
    else:
        ans.append([f.A[f.middle], f.min_fx])

for row in ans:
    print(row[0], row[1])

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)


def main():
    q = I()
    aa = [LI() for _ in range(q)]

    bt = aa[0][2]
    x = [[-inf, 1], [aa[0][1], 1], [inf, 1]]
    xi = 1
    xl = 0
    lc = 0
    xr = 0
    rc = 0
    r = []
    for t in aa[1:]:
        if t[0] == 2:
            r.append('{} {}'.format(x[xi][0], xl+xr+bt))
        else:
            _,a,b = t
            bt += b
            xa = x[xi]
            if xa[0] == a:
                x[xi][1] += 1
                continue
            bi = bisect.bisect_left(x, [a, 1])
            if x[bi][0] == 0:
                x[bi][1]
            else:
                if bi <= xi:
                    xi += 1
                x.insert(bi, [a, 1])
            if bi < xi:
                xl += xa[0] - a
                lc += 1
                t = xl + xr
                na = x[xi-1]
                s = xa[0] - na[0]
                nt = xl - lc * s + xr + (rc+xa[1]) * s
                if nt <= t:
                    xl -= lc * s
                    xr += (rc+xa[1]) * s
                    lc -= na[1]
                    rc += xa[1]
                    xi -= 1
            else:
                xr += a - xa[0]
                rc += 1
                t = xl + xr
                na = x[xi+1]
                s = na[0] - xa[0]
                nt = xl + (lc+xa[1]) * s + xr - rc * s
                if nt < t:
                    xl += (lc+xa[1]) * s
                    xr -= rc * s
                    lc += xa[1]
                    rc -= na[1]
                    xi += 1

    return '\n'.join(r)


print(main())

Submission status: Wrong Answer

Solution code:
Q = int(input())
INF = 10 ** 9

queries = []
for _ in range(Q):
    queries.append(input())

from bisect import insort_left
from heapq import heappush

ary, absum, bsum, c = [], 0, 0, 0
left, right = -INF, INF
for query in queries:
    if query[0] == '2':
        # absum = sum(abs(a - ary[(c-1)//2]) for a in ary)
        # absum = asum - (c//2 + 1) * ary[(c-1)//2]
        # print(' '.join(map(str, (ary[(c-1)//2], absum + bsum))))
        print(str(ary[(c-1)//2]) + ' ' + str(absum + bsum))
    else:
        _, a, b = map(int, query.split())
        # insort_left(ary, a)
        heappush(ary, a)
        c += 1
        if left <= a <= right:
            absum += 0
        elif a < left:
            absum += left - a
        else:
            absum += a - right
        left, right = ary[(c-1)//2], ary[c - (c-1)//2 - 1]        
        bsum += b

Submission status: Wrong Answer

Solution code:
n=int(input())
def bin_insert(x,r):
    low = 0
    high = r
    mid = r // 2
    while low < high:
        if l[mid] > x:
            high = mid
        else:
            low = mid+1
        mid = (low + high) // 2
    l.insert(mid, x)
    return mid

In=list(map(int,input().split()))
l=[In[1]]
r=1
b=In[2]
x=In[1]
m=0
for _ in range(n-1):
    In=list(map(int,input().split()))
    if In[0]==1:
        index = bin_insert(In[1],r)
        r+=1
        b+=In[2]
        pre_x=x
        x=l[(r-1)//2]
        m+=abs(pre_x-In[1])
    else:
        print(x, m+b)

Submission status: Wrong Answer

Solution code:
n=int(input())
def bin_insert(x,r):
    low = 0
    high = r
    mid = r // 2
    while low < high:
        if l[mid] > x:
            high = mid-1
        else:
            low = mid + 1
        mid = (low + high) // 2
    l.insert(mid, x)
    return mid

In=list(map(int,input().split()))
l=[In[1]]
r=1
b=In[2]
x=In[1]
m=0
for _ in range(n-1):
    In=list(map(int,input().split()))
    if In[0]==1:
        index = bin_insert(In[1],r)
        r+=1
        b+=In[2]
        pre_x=x
        x=l[(r-1)//2]
        m+=abs(pre_x-In[1])
    else:
        print(x, m+b)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys
from heapq import heappush

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

Q=INT()

li=[]
A=B=fx=cost=cnt=l=r=0
for _ in range(Q):
    instr=input()
    if instr[0]=='1':
        _,a,b=map(int, instr.split())
        B+=b
        heappush(li, a)
        if cnt==0:
            l=r=A=a
        else:
            if not (l<=a<=r):
                cost+=min(abs(l-a), abs(r-a))
            # 偶数回目
            if cnt%2==1:
                A=l=li[len(li)//2-1]
                r=li[len(li)//2]
            # 奇数回目
            else:
                A=l=r=li[len(li)//2]
        fx=cost+B
        cnt+=1
    else:
        print(A, fx)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys
from heapq import heappush

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

Q=INT()

li=[]
A=B=fx=cost=cnt=l=r=0
for _ in range(Q):
    instr=input()
    if instr[0]=='1':
        _,a,b=map(int, instr.split())
        B+=b
        heappush(li, a)
        if cnt==0:
            l=r=A=a
        else:
            # 偶数回目
            if cnt%2==1:
                A2=li[len(li)//2-1]
                if l!=A2:
                    cost+=abs(l-A2)
                l=li[len(li)//2-1]
                r=li[len(li)//2]
            # 奇数回目
            else:
                A2=li[len(li)//2]
                if not (l<=A2<=r):
                    cost+=min(abs(l-A2), abs(r-A2))
                l=r=li[len(li)//2]
            A=A2
        fx=cost+B
        cnt+=1
    else:
        print(A, fx)

Submission status: Wrong Answer

Solution code:
import heapq
N = int(input())
st = input()
n, a, b = map(int,st.split())

minvalue = b
left = [-a]
right = [a]
ptnum = 1
for i in range(N-1):
  st = input()
  if st[0] == '1':#入力クエリ
    n, a, b = map(int,st.split())
    if ptnum%2 == 1:#lrは一致
      l = - heapq.heappop(left)
      r = heapq.heappop(right)
      le = min(l,a)
      ri = max(a,r)
      heapq.heappush(left, -le)
      heapq.heappush(right, ri)
      minvalue += abs(l-a)
    else:#lr は必ずしも一致しない
      if a < -left[0]:#中央がlに
        heapq.heappush(left, -a)
        heapq.heappush(right, -left[0])
        minvalue -= a+left[0]
      elif a > right[0]:#中央がrに
        heapq.heappush(left, -right[0])
        heapq.heappush(right, a)
        minvalue += a-right[0]
      else:
        heapq.heappush(left, -a)
        heapq.heappush(right, a)
        
    minvalue += b
      
  else:
    print(- left[0], minvalue)

Submission status: Wrong Answer

Solution code:
import heapq
N = int(input())
st = input()
n, a, b = map(int,st.split())

minvalue = b
left = [-a]
right = [a]
ptnum = 1
for i in range(N-1):
  st = input()
  if st[0] == '1':#入力クエリ
    n, a, b = map(int,st.split())
    if ptnum%2 == 1:#lrは一致
      l = - heapq.heappop(left)
      r = heapq.heappop(right)
      le = min(l,a)
      ri = max(a,r)
      heapq.heappush(left, -le)
      heapq.heappush(right, ri)
      minvalue += abs(l+left[0])
    else:#lr は必ずしも一致しない
      if a < -left[0]:#中央がlに
        heapq.heappush(left, -a)
        heapq.heappush(right, -left[0])
        minvalue -= a+left[0]
      elif a > right[0]:#中央がrに
        heapq.heappush(left, -right[0])
        heapq.heappush(right, a)
        minvalue += a-right[0]
      else:
        heapq.heappush(left, -a)
        heapq.heappush(right, a)
        
    minvalue += b
      
  else:
    print(- left[0], minvalue)

Submission status: Wrong Answer

Solution code:
import heapq
N = int(input())
st = input()
n, a, b = map(int,st.split())

minvalue = b
left = [-a]
right = [a]
ptnum = 1
for i in range(N-1):
  st = input()
  if st[0] == '1':#入力クエリ
    n, a, b = map(int,st.split())
    if ptnum%2 == 1:#lrは一致
      l = - heapq.heappop(left)
      r = heapq.heappop(right)
      le = min(l,a)
      ri = max(a,r)
      heapq.heappush(left, -le)
      heapq.heappush(right, ri)
      minvalue += max(l+left[0], 0)
    else:#lr は必ずしも一致しない
      if a < -left[0]:#中央がlに
        heapq.heappush(left, -a)
        heapq.heappush(right, -left[0])
        minvalue -= a+left[0]
      elif a > right[0]:#中央がrに
        heapq.heappush(left, -right[0])
        heapq.heappush(right, a)
        minvalue += a-right[0]
      else:
        heapq.heappush(left, -a)
        heapq.heappush(right, a)
        
    minvalue += b
      
  else:
    print(- left[0], minvalue)

Submission status: Wrong Answer

Solution code:
import heapq
N = int(input())
st = input()
n, a, b = map(int,st.split())

minvalue = b
left = [-a]
right = [a]
ptnum = 1
for i in range(N-1):
  st = input()
  if st[0] == '1':#入力クエリ
    n, a, b = map(int,st.split())
    if ptnum%2 == 1:#lrは一致
      l = - heapq.heappop(left)
      r = heapq.heappop(right)
      le = min(l,a)
      ri = max(a,r)
      heapq.heappush(left, -le)
      heapq.heappush(right, ri)
      minvalue += max(l+left[0], 0)
    else:#lr は必ずしも一致しない
      if a < -left[0]:#中央がlに
        heapq.heappush(left, -a)
        heapq.heappush(right, -left[0])
        minvalue -= a+left[0]
      elif b > right[0]:#中央がrに
        heapq.heappush(left, -right[0])
        heapq.heappush(right, a)
        minvalue += a-right[0]
      else:
        heapq.heappush(left, -a)
        heapq.heappush(right, a)
        
    minvalue += b
      
  else:
    print(- left[0], minvalue)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc127/tasks/abc127_f
import sys
from heapq import heappush, heappop


class StreamMedian(object):
    def __init__(self):
        self.left = []
        self.right = []

    def __repr__(self):
        return "left: {},  right: {}".format(self.left, self.right)

    def __len__(self):
        return len(self.left) + len(self.right)

    def push(self, x):
        if not self.left:
            self._push_left(x)
        else:
            if x < self._peek_left():
                self._push_left(x)
                if len(self.left) + 1 > len(self.right):
                    self._push_right(self._pop_left())
            else:
                self._push_right(x)
                if len(self.left) < len(self.right):
                    self._push_left(self._pop_right())

    def _push_left(self, x):
        heappush(self.left, -x)

    def _push_right(self, x):
        heappush(self.right, x)

    def _pop_left(self):
        return -heappop(self.left)

    def _pop_right(self):
        return heappop(self.right)

    def _peek_left(self):
        return -self.left[0]

    def is_empty(self):
        return len(self.left) == 0

    def get(self):
        if len(self.left) == len(self.right):
            res = [-self.left[0], self.right[0]]
        else:
            res = [-self.left[0]]
        return res


Q = int(input())
box = StreamMedian()
offset = 0
for _ in range(Q):
    query = input()
    if query[0] == "1":
        a, b = map(int, query[2:].split())
        if box.is_empty():
            delta = 0
        elif len(box.get()) == 1:
            delta = next(abs(a - k) for k in box.get())
        else:
            sm, lg = box.get()
            if sm <= a <= lg:
                delta = 0
            else:
                delta = min(abs(a - k) for k in box.get())
        offset += b + delta
        box.push(a)
        # print(box, file=sys.stderr)
    else:
        x = box.get()[0]
        print(x, offset)

Submission status: Wrong Answer

Solution code:
from heapq import*
L,R=[],[]
B=t=0
Q,*E=open(0)
for e in E:
	if' 'in e:_,a,b=map(int,e.split());t=1-t;heappush([R,L][t],-heappushpop([L,R][t],[-a,a][t]));B+=b
	else:print(-L[0],B+(0 if t else L[0]+R[0]))

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:
# https://atcoder.jp/contests/abc127/tasks/abc127_f
import sys
from heapq import heappush, heappop


class StreamMedian(object):
    def __init__(self):
        self.left = []
        self.right = []

    def __repr__(self):
        return "left: {},  right: {}".format(self.left, self.right)

    def __len__(self):
        return len(self.left) + len(self.right)

    def push(self, x):
        if not self.left:
            self._push_left(x)
        else:
            if x < self._peek_left():
                self._push_left(x)
                if len(self.left) + 1 > len(self.right):
                    self._push_right(self._pop_left())
            else:
                self._push_right(x)
                if len(self.left) < len(self.right):
                    self._push_left(self._pop_right())

    def _push_left(self, x):
        heappush(self.left, -x)

    def _push_right(self, x):
        heappush(self.right, x)

    def _pop_left(self):
        return -heappop(self.left)

    def _pop_right(self):
        return heappop(self.right)

    def _peek_left(self):
        return -self.left[0]

    def is_empty(self):
        return len(self.left) == 0

    def get(self):
        if len(self.left) == len(self.right):
            res = [-self.left[0], self.right[0]]
        else:
            res = [-self.left[0]]
        return res


Q = int(input())
box = StreamMedian()
offset = 0
for _ in range(Q):
    query = input()
    if query[0] == "1":
        a, b = map(int, query[2:].split())
        if box.is_empty():
            delta = 0
        else:
            delta = min(abs(a - k) for k in box.get())
        offset += b + delta
        box.push(a)
        # print(box, file=sys.stderr)
    else:
        x = box.get()[0]
        print(x, offset)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc127/tasks/abc127_f
import sys
from heapq import heappush, heappop


class StreamMedian(object):
    def __init__(self):
        self.left = []
        self.right = []

    def __repr__(self):
        return "left: {},  right: {}".format(self.left, self.right)

    def __len__(self):
        return len(self.left) + len(self.right)

    def push(self, x):
        if not self.left:
            self._push_left(x)
        else:
            if x < self._peek_left():
                self._push_left(x)
                if len(self.left) + 1 > len(self.right):
                    self._push_right(self._pop_left())
            else:
                self._push_right(x)
                if len(self.left) < len(self.right):
                    self._push_left(self._pop_right())

    def _push_left(self, x):
        heappush(self.left, -x)

    def _push_right(self, x):
        heappush(self.right, x)

    def _pop_left(self):
        return -heappop(self.left)

    def _pop_right(self):
        return heappop(self.right)

    def _peek_left(self):
        return -self.left[0]

    def is_empty(self):
        return len(self.left) == 0

    def get(self):
        if len(self.left) == len(self.right):
            res = [-self.left[0], self.right[0]]
        else:
            res = [-self.left[0]]
        return res


Q = int(input())
box = StreamMedian()
offset = 0
for _ in range(Q):
    query = input()
    if query[0] == "1":
        a, b = map(int, query[2:].split())
        if box.is_empty():
            delta = 0
        else:
            delta = min(abs(a - k) for k in box.get())
        offset += b + delta
        box.push(a)
        # print(box, file=sys.stderr)
    else:
        print(box.get(), offset)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc127/tasks/abc127_f
import sys
from heapq import heappush, heappop


class StreamMedian(object):
    def __init__(self):
        self.left = []
        self.right = []

    def __repr__(self):
        return "left: {},  right: {}".format(self.left, self.right)

    def push(self, x):
        if not self.left:
            self._push_left(x)
        else:
            if x < self._peek_left():
                self._push_left(x)
                if len(self.left) + 1 > len(self.right):
                    self._push_right(self._pop_left())
            else:
                self._push_right(x)
                if len(self.left) < len(self.right):
                    self._push_left(self._pop_right())

    def _push_left(self, x):
        heappush(self.left, -x)

    def _push_right(self, x):
        heappush(self.right, x)

    def _pop_left(self):
        return -heappop(self.left)

    def _pop_right(self):
        return heappop(self.right)

    def _peek_left(self):
        return self.get()

    def is_empty(self):
        return len(self.left) == 0

    def get(self):
        return -self.left[0]


Q = int(input())
box = StreamMedian()
offset = 0
for _ in range(Q):
    query = input()
    if query[0] == "1":
        a, b = map(int, query[2:].split())
        if box.is_empty():
            delta = 0
        else:
            delta = abs(a - box.get())
        offset += b + delta
        box.push(a)
        # print(box, file=sys.stderr)
    else:
        print(box.get(), offset)

Submission status: Wrong Answer

Solution code:
import bisect

Q = int(input())

a = []
a_new = []
b_sum = 0
minima = 0
argmin = 10**10
shift = 0
for i in range(Q):
    ipt = list(map(int, input().split()))
    if len(ipt) == 3:
        if len(a) == 0:
            a.append(ipt[1])
        else:
            a.insert(bisect.bisect_left(a, ipt[1]), ipt[1])
            if ipt[1] < argmin:
                shift += 1
            else:
                shift -= 1
        a_new.append(ipt[1])
        b_sum += ipt[2]
    else:
        argmin_new = a[(len(a) - 1) // 2]
        if argmin == 10**10:
            argmin = argmin_new
        minima += sum([abs(argmin_new - anew) for anew in a_new]) + (abs(argmin_new - argmin) * abs(shift))
        argmin = argmin_new
        a_new = []
        shift = 0
        print(argmin, minima + b_sum)

Submission status: Wrong Answer

Solution code:
Q = int(input())

import heapq

INF = 10**18
L = []
R = []
heapq.heappush(L,INF)
heapq.heappush(R,INF)
L_size = 0
R_size = 0
sum_b = 0
min_x = 0
sum_L = 0
sum_R = 0

def f1(a,b):
  global sum_b,L_size,R_size,min_x,sum_L,sum_R
  sum_b += b
  L_max = -heapq.heappop(L)
  R_min = heapq.heappop(R)
  sum_L -= L_max
  sum_R -= R_min
  X = sorted([L_max,a,R_min])
  if L_size == R_size:
    heapq.heappush(L,-X[0])
    heapq.heappush(L,-X[1])
    heapq.heappush(R,X[2])
    L_size += 1
    sum_L += X[0] + X[1]
    sum_R += X[2]
    min_x = X[1]
  else:
    heapq.heappush(L,-X[0])
    heapq.heappush(R,X[1])
    heapq.heappush(R,X[2])
    R_size += 1
    sum_L += X[0]
    sum_R += X[1] + X[2]
    min_x = X[0]
  return

def f2():
  min_value = sum_b
  if L_size == R_size:
    min_value += sum_R - sum_L
  else:
    min_value += sum_R - sum_L + min_x
  print(min_x,min_value)

for _ in range(Q):
  row = [int(x) for x in input().split()]
  if row[0] == 1:
    f1(row[1],row[2])
  else:
    f2()