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:
from bisect import bisect_left
q=int(input())
query=[input() for _ in range(q)]
l,r=-pow(10,9),pow(10,9)
ary=[-float('inf')]
for qq in query:
    if qq[0]=='1':
        _,a,b=map(int,qq.split())
        ary.append(a)
ary.append(float('inf'))
ary.sort()

class SegmentTree():
    def __init__(self,size,segfunc=lambda x,y:x+y,default=0):
        self.segfunc=segfunc
        self.default=default
        self.size=pow(2,(size-1).bit_length())
        self.data=[default]*(self.size*2)
    def update(self,i,v):
        i+=self.size
        self.data[i]=v
        while i:
            i>>=1
            self.data[i]=self.segfunc(self.data[2*i],self.data[2*i+1])
    # 区間[l,r)に対するクエリ
    def query(self,l,r):
        l+=self.size
        r+=self.size
        lres,rres=self.default,self.default
        while l<r:
            if l&1:
                lres=self.segfunc(lres, self.data[l])
                l += 1
            if r&1:
                r-=1
                rres=self.segfunc(self.data[r],rres)
            l>>=1
            r>>=1
        res=self.segfunc(lres,rres)
        return res
    def get(self,i):
        return self.data[self.size+i]
    def add(self,i,v):
        self.update(i,self.get(i)+v)
    
#ary[0],ary[-1]
ml,mr=0,len(ary)+1
mv=0
st=SegmentTree(len(ary))
for qq in query:
    if qq[0]=='2':
        print(ary[ml],mv)
    else:
        _,a,b=map(int,qq.split())
        a=bisect_left(ary,a)
        st.add(0,-1)
        st.add(a,2)
        if ml<=a<mr:
            mv+=b
            ml,mr=a,a+1
        else:
            l,r=0,len(ary)+1
            mv+=min(abs(ary[ml]-ary[a]),abs(ary[mr-1]-ary[a]))
            mv+=b
            while r-l>1:
                x=(l+r)//2
                chk=st.query(0,x)
                if chk>=0:
                    l,r=l,x
                else:
                    l,r=x,r
            ml=l if st.query(0,l+1)>=0 else l+1
            l,r=ml,len(ary)+1
            while r-l>1:
                x=(l+r)//2
                chk=st.query(0,x)
                if chk>0:
                    l,r=l,x
                else:
                    l,r=x,r
            mr=l if st.query(0,l+1)>0 else l+1

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
q=int(input())
query=[input() for _ in range(q)]
l,r=-pow(10,9),pow(10,9)
ary=[-float('inf')]
for qq in query:
    if qq[0]=='1':
        _,a,b=map(int,qq.split())
        ary.append(a)
ary.append(float('inf'))
ary.sort()

class SegmentTree():
    def __init__(self,size,segfunc=lambda x,y:x+y,default=0):
        self.segfunc=segfunc
        self.default=default
        self.size=pow(2,(size-1).bit_length())
        self.data=[default]*(self.size*2)
    def update(self,i,v):
        i+=self.size
        self.data[i]=v
        while i:
            i>>=1
            self.data[i]=self.segfunc(self.data[2*i],self.data[2*i+1])
    # 区間[l,r)に対するクエリ
    def query(self,l,r):
        l+=self.size
        r+=self.size
        lres,rres=self.default,self.default
        while l<r:
            if l&1:
                lres=self.segfunc(lres, self.data[l])
                l += 1
            if r&1:
                r-=1
                rres=self.segfunc(self.data[r],rres)
            l>>=1
            r>>=1
        res=self.segfunc(lres,rres)
        return res
    def get(self,i):
        return self.data[self.size+i]
    def add(self,i,v):
        self.update(i,self.get(i)+v)
    
#ary[0],ary[-1]
ml,mr=0,len(ary)+1
mv=0
st=SegmentTree(len(ary))
for qq in query:
    if qq[0]=='2':
        print(ary[ml],mv)
    else:
        _,a,b=map(int,qq.split())
        a=bisect_left(ary,a)
        st.add(0,-1)
        st.add(a,2)
        if ml<=a<mr:
            mv+=b
            ml,mr=a,a+1
        else:
            l,r=0,len(ary)+1
            mv+=min(abs(ary[ml]-ary[a]),abs(ary[mr]-ary[a]))
            mv+=b
            while r-l>1:
                x=(l+r)//2
                chk=st.query(0,x)
                if chk>=0:
                    l,r=l,x
                else:
                    l,r=x,r
            ml=l if st.query(0,l+1)>=0 else l+1
            l,r=ml,len(ary)+1
            while r-l>1:
                x=(l+r)//2
                chk=st.query(0,x)
                if chk>0:
                    l,r=l,x
                else:
                    l,r=x,r
            mr=l if st.query(0,l+1)>0 else l+1

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
q=int(input())
query=[input() for _ in range(q)]
l,r=-pow(10,9),pow(10,9)
ary=[-float('inf')]
for qq in query:
    if qq[0]=='1':
        _,a,b=map(int,qq.split())
        ary.append(a)
ary.append(float('inf'))
ary.sort()

class SegmentTree():
    def __init__(self,size,segfunc=lambda x,y:x+y,default=0):
        self.segfunc=segfunc
        self.default=default
        self.size=pow(2,(size-1).bit_length())
        self.data=[default]*(self.size*2)
    def update(self,i,v):
        i+=self.size
        self.data[i]=v
        while i:
            i>>=1
            self.data[i]=self.segfunc(self.data[2*i],self.data[2*i+1])
    # 区間[l,r)に対するクエリ
    def query(self,l,r):
        l+=self.size
        r+=self.size
        lres,rres=self.default,self.default
        while l<r:
            if l&1:
                lres=self.segfunc(lres, self.data[l])
                l += 1
            if r&1:
                r-=1
                rres=self.segfunc(self.data[r],rres)
            l>>=1
            r>>=1
        res=self.segfunc(lres,rres)
        return res
    def get(self,i):
        return self.data[self.size+i]
    def add(self,i,v):
        self.update(i,self.get(i)+v)
    
#ary[0],ary[-1]
ml,mr=0,len(ary)+1
mv=0
st=SegmentTree(len(ary))
for qq in query:
    if qq[0]=='2':
        print(ary[ml],mv)
    else:
        _,a,b=map(int,qq.split())
        a=bisect_left(ary,a)
        st.add(0,-1)
        st.add(a,2)
        if ml<=a<mr:
            mv+=b
            ml,mr=a,a+1
        else:
            l,r=0,len(ary)+1
            mv+=min(abs(ary[ml]-ary[a]),abs(ary[mr]-ary[a]))
            mv+=b
            while r-l>1:
                x=(l+r)//2
                chk=st.query(0,x)
                if chk>=0:
                    l,r=l,x
                else:
                    l,r=x,r
            ml=l
            l,r=ml,len(ary)+1
            while r-l>1:
                x=(l+r)//2
                chk=st.query(0,x)
                if chk>0:
                    l,r=l,x
                else:
                    l,r=x,r
            mr=l

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
q=int(input())
query=[input() for _ in range(q)]
l,r=-pow(10,9),pow(10,9)
ary=[-float('inf')]
for qq in query:
    if qq[0]=='1':
        _,a,b=map(int,qq.split())
        ary.append(a)
ary.append(float('inf'))
ary.sort()

class SegmentTree():
    def __init__(self,size,segfunc=lambda x,y:x+y,default=0):
        self.segfunc=segfunc
        self.default=default
        self.size=pow(2,(size-1).bit_length())
        self.data=[default]*(self.size*2)
    def update(self,i,v):
        i+=self.size
        self.data[i]=v
        while i:
            i>>=1
            self.data[i]=self.segfunc(self.data[2*i],self.data[2*i+1])
    # 区間[l,r)に対するクエリ
    def query(self,l,r):
        l+=self.size
        r+=self.size
        lres,rres=self.default,self.default
        while l<r:
            if l&1:
                lres=self.segfunc(lres, self.data[l])
                l += 1
            if r&1:
                r-=1
                rres=self.segfunc(self.data[r],rres)
            l>>=1
            r>>=1
        res=self.segfunc(lres,rres)
        return res
#ary[0],ary[-1]
ml,mr=0,len(ary)+1
mv=0
st=SegmentTree(len(ary))
for qq in query:
    if qq[0]=='2':
        print(ary[ml],mv)
    else:
        _,a,b=map(int,qq.split())
        a=bisect_left(ary,a)
        v0=st.data[st.size]
        v1=st.data[st.size+a]
        st.update(0,v0-1)
        st.update(a,v1+2)
        if ml<=a<mr:
            mv+=b
            ml,mr=a,a+1
        else:
            l,r=0,len(ary)+1
            while r-l>1:
                x=(l+r)//2
                chk=st.query(0,x)
                if chk>=0:
                    l,r=l,x
                else:
                    l,r=x,r
            #ml=l #if st.query(0,l)>0 else r
            mv+=b
            mv+=abs(ary[l]-ary[ml])
            ml=l
            l,r=ml,len(ary)+1
            while r-l>1:
                x=(l+r)//2
                chk=st.query(0,x)
                if chk>0:
                    l,r=l,x
                else:
                    l,r=x,r
            mr=l

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
q=int(input())
query=[input() for _ in range(q)]
l,r=-pow(10,9),pow(10,9)
ary=[-float('inf')]
for qq in query:
    if qq[0]=='1':
        _,a,b=map(int,qq.split())
        ary.append(a)
ary.append(float('inf'))
ary.sort()

class SegmentTree():
    def __init__(self,size,segfunc=lambda x,y:x+y,default=0):
        self.segfunc=segfunc
        self.default=default
        self.size=pow(2,(size-1).bit_length())
        self.data=[default]*(self.size*2)
    def update(self,i,v):
        i+=self.size
        self.data[i]=v
        while i:
            i>>=1
            self.data[i]=self.segfunc(self.data[2*i],self.data[2*i+1])
    # 区間[l,r)に対するクエリ
    def query(self,l,r):
        l+=self.size
        r+=self.size
        lres,rres=self.default,self.default
        while l<r:
            if l&1:
                lres=self.segfunc(lres, self.data[l])
                l += 1
            if r&1:
                r-=1
                rres=self.segfunc(self.data[r],rres)
            l>>=1
            r>>=1
        res=self.segfunc(lres,rres)
        return res
#ary[0],ary[-1]
ml,mr=0,len(ary)+1
mv=0
st=SegmentTree(len(ary))
for qq in query:
    if qq[0]=='2':
        print(ary[ml],mv)
    else:
        _,a,b=map(int,qq.split())
        a=bisect_left(ary,a)
        st.update(0,-1)
        st.update(a,2)
        if ml<=a<mr:
            mv+=b
            ml,mr=a,a+1
        elif a<ml:
            l,r=0,ml
            while r-l>1:
                x=(l+r)//2
                chk=st.query(0,x)
                if chk>=0:
                    l,r=l,x
                else:
                    l,r=x,r
            ni=l #if st.query(0,l)>=0 else r
            mv+=b
            mv+=abs(ary[ml]-ary[ni])
            ml,mr=ni,l
        else:
            l,r=mr-1,len(ary)+1
            while r-l>1:
                x=(l+r)//2
                chk=st.query(0,x)
                if chk>=0:
                    l,r=l,x
                else:
                    l,r=x,r
            ni=l #if st.query(0,l)>0 else r
            mv+=b
            mv+=abs(ary[mr-1]-ary[ni])
            ml,mr=r,ni

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
q=int(input())
query=[input() for _ in range(q)]
l,r=-pow(10,9),pow(10,9)
ary=[]
for qq in query:
    if qq[0]=='1':
        _,a,b=map(int,qq.split())
        ary.append(a)
ary.sort()

class SegmentTree():
    def __init__(self,size,segfunc=lambda x,y:x+y,default=0):
        self.segfunc=segfunc
        self.default=default
        self.size=pow(2,(size-1).bit_length())
        self.data=[default]*(self.size*2)
    def update(self,i,v):
        i+=self.size
        self.data[i]=v
        while i:
            i>>=1
            self.data[i]=self.segfunc(self.data[2*i],self.data[2*i+1])
    # 区間[l,r)に対するクエリ
    def query(self,l,r):
        l+=self.size
        r+=self.size
        lres,rres=self.default,self.default
        while l<r:
            if l&1:
                lres=self.segfunc(lres, self.data[l])
                l += 1
            if r&1:
                r-=1
                rres=self.segfunc(self.data[r],rres)
            l>>=1
            r>>=1
        res=self.segfunc(lres,rres)
        return res
#ary[0],ary[-1]
ml,mr=0,len(ary)+1
mv=0
st=SegmentTree(len(ary))
for qq in query:
    if qq[0]=='2':
        print(ary[ml],mv)
    else:
        _,a,b=map(int,qq.split())
        a=bisect_left(ary,a)
        st.update(0,-1)
        st.update(a,2)
        if ml<=a<mr:
            mv+=b
            ml,mr=a,a+1
        elif a<ml:
            l,r=0,ml
            while r-l>1:
                x=(l+r)//2
                chk=st.query(0,x)
                if chk>=0:
                    l,r=l,x
                else:
                    l,r=x,r
            ni=l #if st.query(0,l)>=0 else r
            mv+=b
            mv+=abs(ary[ml]-ary[ni])
            ml,mr=ni,l
        else:
            l,r=mr,len(ary)+1
            while r-l>1:
                x=(l+r)//2
                chk=st.query(0,x)
                if chk>0:
                    l,r=l,x
                else:
                    l,r=x,r
            ni=l #if st.query(0,l)>0 else r
            mv+=b
            mv+=abs(ary[mr-1]-ary[ni])
            ml,mr=r,ni

Submission status: Wrong Answer

Solution code:
q=int(input())
query=[input() for _ in range(q)]
l,r=pow(10,9),-pow(10,9)
arya=[]
sumb=0
def f(x):
    ret=0
    for y in arya:
        ret+=abs(x-y)
    return ret+sumb
for qq in query:
    if qq[0]=='1':
        _,a,b=map(int,qq.split())
        arya.append(a)
        sumb+=b
        l,r=min(l,a),max(r,a)+1
    else:
        l0,r0=l,r
        while r0-l0>1:
            t0=(3*l0+r0)//4
            t1=(2*l0+2*r0)//4
            t2=(l0+3*r0)//4
            v0,v1,v2=f(t0),f(t1),f(t2)
            if v0==v1==f((v0+v1)//2) or v1==v2==f((v1+v2)//2):
                if v0==v1:
                    l0,r0=l,t1
                else:
                    l0,r0=l,t2
                while r0-l0>1:
                    x=(l0+r0)//2
                    if f(x)==v1:
                        l0,r0=l0,x
                    else:
                        l0,r0=x,r0
            elif min(v0,v1,v2)==v0:
                l0,r0=l0,t1
            elif min(v0,v1,v2)==v1:
                l0,r0=t0,t2
            elif min(v0,v1,v2)==v2:
                l0,r0=t2,r0
        if f(l0)<=f(r0):
            print(l0,f(l0))
        else:
            print(r0,f(r0))

Submission status: Wrong Answer

Solution code:
q=int(input())
query=[input() for _ in range(q)]
l,r=pow(10,9),-pow(10,9)
arya=[]
sumb=0
def f(x):
    ret=0
    for y in arya:
        ret+=abs(x-y)
    return ret+sumb
for qq in query:
    if qq[0]=='1':
        _,a,b=map(int,qq.split())
        arya.append(a)
        sumb+=b
        l,r=min(l,a),max(r,a)+1
    else:
        l0,r0=l,r
        while r0-l0>1:
            t0=(3*l0+r0)//4
            t1=(2*l0+2*r0)//4
            t2=(l0+3*r0)//4
            v0,v1,v2=f(t0),f(t1),f(t2)
            if v0==v1==f((v0+v1)//2) or v1==v2==f((v1+v2)//2):
                if v0==v1:
                    l0,r0=l0,t1
                else:
                    l0,r0=l0,t2
                while r0-l0>1:
                    x=(l0+r0)//2
                    if f(x)==v1:
                        l0,r0=l0,x
                    else:
                        l0,r0=x,r0
            elif min(v0,v1,v2)==v0:
                l0,r0=l0,t1
            elif min(v0,v1,v2)==v1:
                l0,r0=t0,t2
            elif min(v0,v1,v2)==v2:
                l0,r0=t2,r0
        if f(l0)<=f(r0):
            print(l0,f(l0))
        else:
            print(r0,f(r0))

Submission status: Wrong Answer

Solution code:
q=int(input())
query=[input() for _ in range(q)]
l,r=pow(10,9),-pow(10,9)
arya=[]
sumb=0
def f(x):
    ret=0
    for y in arya:
        ret+=abs(x-y)
    return ret+sumb
for qq in query:
    if qq[0]=='1':
        _,a,b=map(int,qq.split())
        arya.append(a)
        sumb+=b
        l,r=min(l,a),max(r,a)+1
    else:
        l0,r0=l,r
        while r0-l0>1:
            t0=(3*l0+r0)//4
            t1=(2*l0+2*r0)//4
            t2=(l0+3*r0)//4
            v0,v1,v2=f(t0),f(t1),f(t2)
            if v0==v1==f((v0+v1)//2) or v1==v2==f((v1+v2)//2):
                if v0==v1:
                    l0,r0=l0,t1
                else:
                    l0,r0=v0,t2
                while r0-l0>1:
                    x=(l0+r0)//2
                    if f(x)==v1:
                        l0,r0=l0,x
                    else:
                        l0,r0=x,r0
            elif min(v0,v1,v2)==v0:
                l0,r0=l0,t1
            elif min(v0,v1,v2)==v1:
                l0,r0=t0,t2
            elif min(v0,v1,v2)==v2:
                l0,r0=t2,r0
        if f(l0)<=f(r0):
            print(l0,f(l0))
        else:
            print(r0,f(r0))

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush

q = int(input())
ans = 0

mid_1 = 0
mid_2 = 0
mid = 0
left_q = []
right_q = []
left_sum = 0
right_sum = 0
const_b = 0
ansl = []

first_flag = True
double_mid = False
for _ in range(q):
    row = list(input().split())
    if row[0] == '2':
        half_n = len(left_q)
        if double_mid:
            q_left_sum = mid_1*half_n - left_sum
            q_right_sum = right_sum - mid_1*half_n
            ans_v = q_left_sum + q_right_sum + (mid_2-mid_1) + const_b
            ans_x = mid_1
            ansl.append((ans_x, ans_v))
        else:
            q_left_sum = mid*half_n - left_sum
            q_right_sum = right_sum - mid*half_n
            ans_v = q_left_sum + q_right_sum + const_b
            ans_x = mid
            ansl.append((ans_x, ans_v))
    
    else:
        a,b = int(row[1]), int(row[2])
        const_b += b

        if first_flag:
            mid = a
            first_flag = False

        elif double_mid:
            double_mid = False
            if a <= mid_1:
                heappush(left_q, a*(-1))
                left_sum += a
                heappush(right_q, mid_2)
                right_sum += mid_2
                mid = mid_1
            elif a >= mid_2:
                heappush(left_q, mid_1*(-1))
                left_sum += mid_1
                heappush(right_q, a)
                right_sum += a
                mid = mid_2
            else:
                heappush(left_q, mid_1*(-1))
                left_sum += mid_1
                heappush(right_q, mid_2)
                right_sum += mid_2
                mid = a
            # print(left_q, mid, right_q)
            # print(left_sum, right_sum)


        else:
            double_mid = True
            if a <= mid:
                heappush(left_q, a*(-1))
                left_sum = a
                mid_1 = heappop(left_q)*(-1)
                left_sum -= mid_1*(-1)
                mid_2 = mid
            else:
                heappush(right_q, a)
                right_sum += a
                mid_1 = mid
                mid_2 = heappop(right_q)
                right_sum -= mid_2
            # print(left_q, mid_1, mid_2, right_q)
            # print(left_sum, right_sum)



for ax,av in ansl:
    print(ax,av)

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush

q = int(input())
ans = 0

mid_1 = 0
mid_2 = 0
mid = 0
left_q = []
right_q = []
left_sum = 0
right_sum = 0
const_b = 0
ansl = []

first_flag = True
double_mid = False
for _ in range(q):
    row = list(input().split())
    if row[0] == '2':
        half_n = len(left_q)
        if double_mid:
            q_left_sum = mid_1*half_n - left_sum
            q_right_sum = right_sum - mid_1*half_n
            ans_v = q_left_sum + q_right_sum + (mid_2-mid_1) + const_b
            ans_x = mid_1
            ansl.append((ans_x, ans_v))
        else:
            q_left_sum = mid*half_n - left_sum
            q_right_sum = right_sum - mid*half_n
            ans_v = q_left_sum + q_right_sum + const_b
            ans_x = mid
            ansl.append((ans_x, ans_v))
    
    else:
        a,b = int(row[1]), int(row[2])
        const_b += b

        if first_flag:
            mid = a
            first_flag = False

        elif double_mid:
            double_mid = False
            if a <= mid_1:
                heappush(left_q, a*(-1))
                left_sum += a
                heappush(right_q, mid_2)
                right_sum += mid_2
                mid = mid_1
            elif a >= mid_2:
                heappush(left_q, mid_1*(-1))
                left_sum += mid_1
                heappush(right_q, a)
                right_sum += a
                mid = mid_2
            else:
                heappush(left_q, mid_1*(-1))
                left_sum += mid_1
                heappush(right_q, mid_2)
                right_sum += mid_2
                mid = a
        else:
            double_mid = True
            if a <= mid:
                heappush(left_q, a*(-1))
                left_sum = a
                mid_1 = heappop(left_q)*(-1)
                left_sum -= mid_1
                mid_2 = mid
            else:
                heappush(right_q, a)
                right_sum += a
                mid_1 = mid
                mid_2 = heappop(right_q)
                right_sum -= mid_2


for ax,av in ansl:
    print(ax,av)

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush

q = int(input())
ans = 0

mid_1 = 0
mid_2 = 0
mid = 0
left_q = []
right_q = []
left_sum = 0
right_sum = 0
const_b = 0
ansl = []

first_flag = True
double_mid = False
for _ in range(q):
    row = list(input().split())
    if row[0] == '2':
        half_n = len(left_q)
        if double_mid:
            q_left_sum = mid_1*half_n - left_sum
            q_right_sum = right_sum - mid_1*half_n
            ans_v = q_left_sum + q_right_sum + (mid_2-mid_1) + const_b
            ans_x = mid_1
            ansl.append((ans_x, ans_v))
        else:
            q_left_sum = mid*half_n - left_sum
            q_right_sum = right_sum - mid*half_n
            ans_v = q_left_sum + q_right_sum + const_b
            ans_x = mid
            ansl.append((ans_x, ans_v))
    
    else:
        a,b = int(row[1]), int(row[2])
        const_b += b

        if first_flag:
            mid = a
            first_flag = False

        elif double_mid:
            double_mid = False
            if a <= mid_1:
                heappush(left_q, a*(-1))
                left_sum += a
                heappush(right_q, mid_2)
                right_sum += mid_2
                mid = mid_1
            elif a >= mid_2:
                heappush(left_q, mid_1*(-1))
                left_sum += mid_1
                heappush(right_q, a)
                right_sum += a
                mid = mid_2
            else:
                heappush(left_q, mid_1*(-1))
                left_sum += mid_1
                heappush(right_q, mid_2)
                right_sum += mid_2
                mid = a
        else:
            double_mid = True
            if a <= mid:
                heappush(left_q, a*(-1))
                mid_1 = heappop(left_q)*(-1)
                mid_2 = mid
            else:
                heappush(right_q, a)
                mid_1 = mid
                mid_2 = heappop(right_q)


for ax,av in ansl:
    print(ax,av)

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush

q = int(input())
ans = 0

mid_1 = 0
mid_2 = 0
mid = 0
left_q = []
right_q = []
left_sum = 0
right_sum = 0
const_b = 0
ansl = []

first_flag = True
double_mid = False
for _ in range(q):
    row = list(input().split())
    if row[0] == '2':
        half_n = len(left_q)
        if double_mid:
            q_left_sum = mid_1*half_n - left_sum
            q_right_sum = right_sum - mid_1*half_n
            ans_v = q_left_sum + q_right_sum + (mid_2-mid_1) + const_b
            ans_x = mid_1
            ansl.append((ans_x, ans_v))
        else:
            q_left_sum = mid*half_n - left_sum
            q_right_sum = right_sum - mid*half_n
            ans_v = q_left_sum + q_right_sum + const_b
            ans_x = mid
            ansl.append((ans_x, ans_v))
    
    else:
        a,b = int(row[1]), int(row[2])
        const_b += b

        if first_flag:
            mid = a
            first_flag = False

        elif double_mid:
            double_mid = False
            if a <= mid_1:
                heappush(left_q, a*(-1))
                left_sum += a
                heappush(right_q, mid_2)
                right_sum += mid_2
                mid = mid_1
            elif a >= mid_2:
                heappush(left_q, mid_1*(-1))
                left_sum += mid_1
                heappush(right_q, a)
                right_sum += a
                mid = mid_2
            else:
                heappush(left_q, mid_1*(-1))
                left_sum += mid_1
                heappush(right_q, mid_2)
                right_sum += mid_2
                mid = a
        else:
            double_mid = True
            if a <= mid:
                mid_1 = a
                mid_2 = mid
            else:
                mid_1 = mid
                mid_2 = a


for ax,av in ansl:
    print(ax,av)

Submission status: Wrong Answer

Solution code:
Q = int(input())

A = []
now_a = 0
now_a_score = 10 ** 10
min_score = 0
for i in range(Q):
  Q1 = list(map(int, input().split()))
  if len(Q1) == 3:#更新
    a, b = Q1[1], Q1[2]
    A.append(a)
    count = 0
    now_a_score += abs(now_a - a)
    for j in range(len(A)):
      count += abs(A[j] - a)
    if count < now_a_score:
      now_a_score = count
      now_a = a
    elif count == now_a_score:
      now_a_score = count
      now_a = min(now_a, a)
    #print(now_a, a, now_a_score)  
    min_score += b
  else:#求値
    print(now_a, min_score + now_a_score)

Submission status: Wrong Answer

Solution code:
import heapq

q = int(input())
big, small = [], []
heapq.heapify(big)
heapq.heapify(small)
sumb = 0
sumdist = 0
i = 0
for _ in range(q):
    query = list(map(int, input().split()))
    if query[0] == 1:
        i += 1
        a, b = query[1], query[2]
        sumb += b
        if i % 2 == 1:
            if i == 1:
                median = a
            else:
                if big[0] <= a:
                    sumdist += abs(a - median)
                    heapq.heappush(big, a)
                    median = heapq.heappop(big)
                elif a <= -small[0]:
                    sumdist += abs(a - median)
                    heapq.heappush(small, -a)
                    median = -heapq.heappop(small)
                else:
                    median = a
        else:
            sumdist += abs(a - median)
            heapq.heappush(big, max(a, median))
            heapq.heappush(small, -min(a, median))
            median = -small[0]
    else:
        print(median, sumdist + sumb)

Submission status: Wrong Answer

Solution code:
Q = int(input())

A, B = [], []
for _ in range(Q):
    query = list(map(int,input().split()))

    if query[0] == 1:
        A.append(query[1])
        B.append(query[2])
    else:
        A.sort()

        m = A[len(A)//2 - 1]
        S = 0
        for a in A:
            S += abs(a-m)
        print(m, end = " ")
        print(S + sum(B))

Submission status: Wrong Answer

Solution code:
def ternary_search(f, x_min, x_max, epsilon=1e-15):
    while True:
        x1,x2 = 1/3*(x_max-x_min)+x_min,2/3*(x_max-x_min)+x_min
        gap = f(x1)-f(x2)
        if gap < 0: x_max=x2
        else: x_min=x1
        if abs(gap) <= epsilon:break
    return (x_max+x_min)/2

Q=int(input())
xmin,xmax=0,0
alist=[]
B=0
def g(x):
  res=0
  for j in range(len(alist)):
    res += abs(x-alist[j])
  return res + B

for i in range(Q):
  q = list(map(int,input().split()))
  if q[0]==2:
    x_opt = int(ternary_search(g,xmin,xmax))
    y_opt = g(x_opt)
    print(x_opt,y_opt)
    continue
    
  if q[0]==1:
  	_, a, b = q
    
  if xmin > a:xmin=a-1
  if xmax < a:xmax=a+1
  alist.append(a)
  B += b