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: Runtime Error

Solution code:
# coding:utf-8

import statistics
import math
import re

def main(params):
    q = params[0]
    queries = params[1:]
    currentB = 0
    currentA = 0
    listA = []
    for i in queries:
        line = i.split()
        if len(line) > 1:
            code, a, b = [int(j) for j in i.split()]
            currentB += b
            listA.append(a)
            target = len(listA)/2
            if len(listA) % 2 == 0:
                target = statistics.median_low(listA)
                target2 = statistics.median_high(listA)
                if a <= target:
                    currentA += abs(a - target) + abs(target - target2)
                elif a >= target2:
                    currentA += abs(a - target2) + abs(target - target2)
            else:
                target = statistics.median(listA)
                currentA += abs(a - target)
        else:
            if len(listA) % 2 == 0:
                target = statistics.median_low(listA)
            else:
                target = statistics.median(listA)
            print target, currentA + currentB

if __name__ == "__main__":
    q = raw_input()
    q = int(q)
    cnt = q # TODO: edit here
    params = [q]
    for i in range(cnt):
        param = raw_input()
        params.append(param)
    main(params)

Submission status: Runtime Error

Solution code:
Q = int(input())

import heapq
import sys
input = sys.stdin.readline

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

def f1(a,b):
  global sum_b,L_size,R_size,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]
  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]
  return

def f2():
  min_value = sum_b
  min_x = -heapq.nsmallest(L,1)[0]
  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):
  q = [int(x) for x in input().split()]
  if q[0]&1:
    f1(q[1],q[2])
  else:
    f2()

Submission status: Runtime Error

Solution code:
Q = int(input())

import heapq
import sys
input = sys.stdin.readline

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

def f1(a,b):
  global sum_b,L_size,R_size,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]
  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]
  return

def f2():
  min_value = sum_b
  min_x = -heapq.nsmallest(L,1)
  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):
  q = [int(x) for x in input().split()]
  if q[0]&1:
    f1(q[1],q[2])
  else:
    f2()

Submission status: Runtime Error

Solution code:
Q = int(input())
q = [list(map(int, input().split())) for _ in range(Q)]

def addT(n):
  k = 0
  while True:
    if T[k][0] >= n:
      T[k][1][1] += 1
      if T[k][1][0] == -1:
        T.append([n, [-1, 0], [-1, 0]])
        T[k][1][0] = t
        break
      else:
       k = T[k][1][0]
    else:
      T[k][2][1] += 1
      if T[k][2][0] == -1:
        T.append([n, [-1, 0], [-1, 0]])
        T[k][2][0] = t
        break
      else:
        k = T[k][2][0]

def getTmed(x, n):
  if T[x][1][1] == n - 1:
    return x
  elif T[x][1][1] > n - 1:
    return getTmed(T[x][1][0], n)
  else:
    return getTmed(T[x][2][0], n - (T[x][1][1] + 1))


T = []
b = 0
su = 0
for i in q:
  if T == []:
    T.append([i[1], [-1, 0], [-1, 0]])
    t = 1
    rk = i[1]
    b = i[2]
  elif i[0] == 2:
    k = getTmed(0, (t + 1) // 2)
    print(T[k][0], su + b)
  else:
    addT(i[1])
    k = getTmed(0, (t + 1) // 2)
    su += abs(T[k][0] - rk)
    rk = T[k][0]
    t += 1
    b += i[2]

Submission status: Runtime Error

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import collections

class Node(object):
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None
        self.parent = None
        self.isright = False

    def add_node(self, node):
        if node.value > self.value:
            if self.right is None:
                self.right = node
                node.parent = self
                node.isright = True
            else:
                self.right.add_node(node)
        else:
            if self.left is None:
                self.left = node
                node.parent = self
                node.isright = False
            else:
                self.left.add_node(node)

    def search_nearest_node(self, value):
        if value > self.value:
            if self.right is None:
                return self
            else:
                return self.right.search_nearest_node(value)
        else:
            if self.left is None:
                return self
            else:
                return self.left.search_nearest_node(value)

    def find_lr_value(self, value):
        nearest = self.search_nearest_node(value)
        if nearest.value > value:
            right = nearest.value
            current = nearest
            while True:
                if current.isright:
                    break
                else:
                    current = current.parent
            left = current.value
            return left, right

        else:
            left = nearest.value
            current = nearest
            while True:
                if not current.isright:
                    break
                else:
                    current = current.parent
            right = current.value
            return left, right

    def calc(self, value):
        result = abs(self.value -value)
        if self.left is not None:
            result += self.left.calc(value)
        if self.right is not None:
            result += self.right.calc(value)
        return result


Q, = map(int, sys.stdin.readline().rstrip().split(" "))
b_all = 0
sum_value = 0
k_value = 0
tree = None
qs = [list(map(int, sys.stdin.readline().rstrip().split(" "))) for _ in range(Q)]
# for _ in range(Q):
#     query = list(map(int, sys.stdin.readline().rstrip().split(" ")))
for query in qs:
    if query[0] == 1:
        _, a, b = query
        b_all += b
        if tree is None:
            tree = Node(a)
        else:
            tree.add_node(Node(a))
        sum_value += a
        k_value += 1
    else:
        weight = sum_value / k_value
        l, r = tree.find_lr_value(weight)
        targets = [int(math.floor(weight)), int(math.ceil(weight)), l, r]
        targets_values = [(target, tree.calc(target) + b_all) for target in targets]
        x,value = min(targets_values, key=lambda x: (x[1], x[0]))
        print(x, value)

exit(0)

Submission status: Runtime Error

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import collections

class Node(object):
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right: Node = None
        self.parent: Node = None
        self.isright = False

    def add_node(self, node):
        if node.value > self.value:
            if self.right is None:
                self.right = node
                node.parent = self
                node.isright = True
            else:
                self.right.add_node(node)
        else:
            if self.left is None:
                self.left = node
                node.parent = self
                node.isright = False
            else:
                self.left.add_node(node)

    def search_nearest_node(self, value):
        if value > self.value:
            if self.right is None:
                return self
            else:
                return self.right.search_nearest_node(value)
        else:
            if self.left is None:
                return self
            else:
                return self.left.search_nearest_node(value)

    def find_lr_value(self, value):
        nearest = self.search_nearest_node(value)
        if nearest.value > value:
            right = nearest.value
            current = nearest
            while True:
                if current.isright:
                    break
                else:
                    current = current.parent
            left = current.value
            return left, right

        else:
            left = nearest.value
            current = nearest
            while True:
                if not current.isright:
                    break
                else:
                    current = current.parent
            right = current.value
            return left, right

    def calc(self, value):
        result = abs(self.value -value)
        if self.left is not None:
            result += self.left.calc(value)
        if self.right is not None:
            result += self.right.calc(value)
        return result


Q, = map(int, sys.stdin.readline().rstrip().split(" "))
b_all = 0
sum_value = 0
k_value = 0
tree = None
qs = [list(map(int, sys.stdin.readline().rstrip().split(" "))) for _ in range(Q)]
# for _ in range(Q):
#     query = list(map(int, sys.stdin.readline().rstrip().split(" ")))
for query in qs:
    print(1, 1)
    # if query[0] == 1:
    #     _, a, b = query
    #     b_all += b
    #     if tree is None:
    #         tree = Node(a)
    #     else:
    #         tree.add_node(Node(a))
    #     sum_value += a
    #     k_value += 1
    # else:
    #     weight = sum_value / k_value
    #     l, r = tree.find_lr_value(weight)
    #     targets = [int(math.floor(weight)), int(math.ceil(weight)), l, r]
    #     targets_values = [(target, tree.calc(target) + b_all) for target in targets]
    #     x,value = min(targets_values, key=lambda x: (x[1], x[0]))
    #     print(x, value)

exit(0)

Submission status: Runtime Error

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import collections

class Node(object):
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right: Node = None
        self.parent: Node = None
        self.isright = False

    def add_node(self, node):
        if node.value > self.value:
            if self.right is None:
                self.right = node
                node.parent = self
                node.isright = True
            else:
                self.right.add_node(node)
        else:
            if self.left is None:
                self.left = node
                node.parent = self
                node.isright = False
            else:
                self.left.add_node(node)

    def search_nearest_node(self, value):
        if value > self.value:
            if self.right is None:
                return self
            else:
                return self.right.search_nearest_node(value)
        else:
            if self.left is None:
                return self
            else:
                return self.left.search_nearest_node(value)

    def find_lr_value(self, value):
        nearest = self.search_nearest_node(value)
        if nearest.value > value:
            right = nearest.value
            current = nearest
            while True:
                if current.isright:
                    break
                else:
                    current = current.parent
            left = current.value
            return left, right

        else:
            left = nearest.value
            current = nearest
            while True:
                if not current.isright:
                    break
                else:
                    current = current.parent
            right = current.value
            return left, right

    def calc(self, value):
        result = abs(self.value -value)
        if self.left is not None:
            result += self.left.calc(value)
        if self.right is not None:
            result += self.right.calc(value)
        return result


Q, = map(int, sys.stdin.readline().rstrip().split(" "))
b_all = 0
sum_value = 0
k_value = 0
tree = None
qs = [list(map(int, sys.stdin.readline().rstrip().split(" "))) for _ in range(Q)]
# for _ in range(Q):
#     query = list(map(int, sys.stdin.readline().rstrip().split(" ")))
for query in qs:
    if query[0] == 1:
        _, a, b = query
        b_all += b
        if tree is None:
            tree = Node(a)
        else:
            tree.add_node(Node(a))
        sum_value += a
        k_value += 1
    else:
        weight = sum_value / k_value
        l, r = tree.find_lr_value(weight)
        targets = [int(math.floor(weight)), int(math.ceil(weight)), l, r]
        targets_values = [(target, tree.calc(target) + b_all) for target in targets]
        x,value = min(targets_values, key=lambda x: (x[1], x[0]))
        print(x, value)

exit(0)

Submission status: Runtime Error

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import collections

class Node(object):
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right: Node = None
        self.parent: Node = None
        self.isright = False

    def add_node(self, node):
        if node.value > self.value:
            if self.right is None:
                self.right = node
                node.parent = self
                node.isright = True
            else:
                self.right.add_node(node)
        else:
            if self.left is None:
                self.left = node
                node.parent = self
                node.isright = False
            else:
                self.left.add_node(node)

    def search_nearest_node(self, value):
        if value > self.value:
            if self.right is None:
                return self
            else:
                return self.right.search_nearest_node(value)
        else:
            if self.left is None:
                return self
            else:
                return self.left.search_nearest_node(value)

    def find_lr_value(self, value):
        nearest = self.search_nearest_node(value)
        if nearest.value > value:
            right = nearest.value
            current = nearest
            while True:
                if current.isright:
                    break
                else:
                    current = current.parent
            left = current.value
            return left, right

        else:
            left = nearest.value
            current = nearest
            while True:
                if not current.isright:
                    break
                else:
                    current = current.parent
            right = current.value
            return left, right

    def calc(self, value):
        result = abs(self.value -value)
        if self.left is not None:
            result += self.left.calc(value)
        if self.right is not None:
            result += self.right.calc(value)
        return result


Q, = map(int, sys.stdin.readline().rstrip().split(" "))
b_all = 0
sum_value = 0
k_value = 0
tree = None
qs = [list(map(int, sys.stdin.readline().rstrip().split(" "))) for _ in range(Q-1)]
# for _ in range(Q):
#     query = list(map(int, sys.stdin.readline().rstrip().split(" ")))
for query in qs:
    if query[0] == 1:
        _, a, b = query
        b_all += b
        if tree is None:
            tree = Node(a)
        else:
            tree.add_node(Node(a))
        sum_value += a
        k_value += 1
    else:
        weight = sum_value / k_value
        l, r = tree.find_lr_value(weight)
        targets = [int(math.floor(weight)), int(math.ceil(weight)), l, r]
        targets_values = [(target, tree.calc(target) + b_all) for target in targets]
        x,value = min(targets_values, key=lambda x: (x[1], x[0]))
        print(x, value)

exit(0)

Submission status: Runtime Error

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import collections

class Node(object):
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right: Node = None
        self.parent: Node = None
        self.isright = False

    def add_node(self, node):
        if node.value > self.value:
            if self.right is None:
                self.right = node
                node.parent = self
                node.isright = True
            else:
                self.right.add_node(node)
        else:
            if self.left is None:
                self.left = node
                node.parent = self
                node.isright = False
            else:
                self.left.add_node(node)

    def search_nearest_node(self, value):
        if value > self.value:
            if self.right is None:
                return self
            else:
                return self.right.search_nearest_node(value)
        else:
            if self.left is None:
                return self
            else:
                return self.left.search_nearest_node(value)

    def find_lr_value(self, value):
        nearest = self.search_nearest_node(value)
        if nearest.value > value:
            right = nearest.value
            current = nearest
            while True:
                if current.isright:
                    break
                else:
                    current = current.parent
            left = current.value
            return left, right

        else:
            left = nearest.value
            current = nearest
            while True:
                if not current.isright:
                    break
                else:
                    current = current.parent
            right = current.value
            return left, right

    def calc(self, value):
        result = abs(self.value -value)
        if self.left is not None:
            result += self.left.calc(value)
        if self.right is not None:
            result += self.right.calc(value)
        return result


Q, = map(int, sys.stdin.readline().rstrip().split(" "))
b_all = 0
sum_value = 0
k_value = 0
tree = None
for _ in range(Q):
    query = list(map(int, sys.stdin.readline().rstrip().split(" ")))
    if query[0] == 1:
        _, a, b = query
        b_all += b
        if tree is None:
            tree = Node(a)
        else:
            tree.add_node(Node(a))
        sum_value += a
        k_value += 1
    else:
        weight = sum_value / k_value
        l, r = tree.find_lr_value(weight)
        targets = [int(math.floor(weight)), int(math.ceil(weight)), l, r]
        targets_values = [(target, tree.calc(target) + b_all) for target in targets]
        x,value = min(targets_values, key=lambda x: (x[1], x[0]))
        print(x, value)

exit(0)

Submission status: Runtime Error

Solution code:
from bisect import bisect_left
import sys
sys.setrecursionlimit(700000)

def f(l1, l2, ls, x):
    p = bisect_left(l1, x)
    return (x*p-l2[p-1])+(ls-l2[p]-x*p)

def moderate(n):
    if n%1 < 5: return n//1
    else: return n//1+1

q = int(input())
s1, s2, cnt = 0, 0, 0
al, asum = [0], [0]
flag = False
for i in range(q):
    query = list(map(int, input().split()))
    if len(query) == 1:
        x = int(moderate(s1/cnt))
        if cnt == 2:
            x = min(al)
        print(x, f(al, asum, s1, x)+s2)
    else:
        a, b = query[1], query[2]
        cnt += 1
        al.append(a)
        s1 += a
        asum.append(s1)
        s2 += b

Submission status: Runtime Error

Solution code:
Q = int(input())
Queries = [list(map(int, input().split())) for _ in range(Q)]

import heapq as hp
L = [10**9+1] # 最小ヒープなので負を入れる
R = [10**9+1] # そのまま
hp.heapify(L)
hp.heapify(R)
odd = False
sum_b = 0
sum_l = 0
sum_r = 0

flag = 1

for q in Queries:
  if len(q)==1 and q[0]==2:
    if flag ==0:
      print(*(-L[0], sum_b + abs(sum_l-sum_r)))
    else:
      print(*(-L[0], sum_b)
      flag = 1

  else:
    _, a, b = q
    sum_b += b

    l = -hp.heappop(L)
    r = hp.heappop(R)
    sum_l -= l
    sum_r -= r
    X = sorted([l, r, a])
    if odd==False:
      hp.heappush(L,-X[0])
      hp.heappush(L,-X[1])
      hp.heappush(R,X[2])
      sum_l += X[0]+X[1]
      sum_r += X[2]
      odd=True
    elif odd==True:
      hp.heappush(L,-X[0])
      hp.heappush(R,X[1])
      hp.heappush(R,X[2])
      sum_l += X[0]
      sum_r += X[1] + X[2]
      odd=False

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
from heapq import heappush, heappop
def solve(): 
    Q = int(input())
    q, a, b = map(int, input().split())
    B = b
    c = a
    L, R = [], []
    count = 1
    x = c
    v_ = 0
    res_l = []

    for _ in range(Q-1):
        *q_, = map(int, input().split())
        if q_[0] == 1:
            q, a, b = q_
            B += b
            if count&1:
                if a < c:
                    heappush(L, -a)
                    heappush(R, c)
                    x_ = -L[0]
                    v_ = x-x_
                    x = x_
                else:
                    heappush(L, -c)
                    heappush(R, a)                
            else:
                if a <= -L[0]:
                    heappush(L, -a)
                    x_ = heappop(L)
                elif a >= R[0]:
                    heappush(R, a)
                    x_ = heappop(R)
                else:
                    x_ = c = a:

            v_ += abs(x-a)
            count ^= 1
        else:
            v = v_ + B
            res_l.append('{} {}'.format(x, v))
    res = '\n'.join(res_l)
    return str(res)

if __name__ == '__main__':
    print(solve())

Submission status: Runtime Error

Solution code:
Q = int(input())
bSum=0
a = []
aSum = 0
for i in range(Q):
    tmp = list(map(int,input().split()))
    if len(tmp) == 1:
        x = aSum//i
        ans = 0
        for k in a:
            ans += abs(a-x)
        print("{0} {1}".format(x, ans))
    else:
        a += tmp[1]
        a.append(tmp[1])
        bSum += tmp[2]

Submission status: Runtime Error

Solution code:
q = int(input())

a = []

def q2(a,b):
    counter = 0
    a.sort()
    while(a):
        if len(a) == 1:
            point = a.pop(0)
        elif len(a) == 2:
            point = a.pop(0)
            r = a.pop(-1)
            counter += r - point
        else:
            r = a.pop(-1)
            l = a.pop(0)
            counter += r-l
    print(point,counter+b)

b = 0
for i in range(q):
    t = list(map(int,input().split()))
    if t[0] == 1:
        a.append(t[1])
        b+=t[2]
    else:
        q2(a,b)

Submission status: Runtime Error

Solution code:
import numpy as np
Q = int(input())
question_count = 1
z = []
A = np.array(z)
ans_1 = []
ans_2 = []
sum_B = 0
while question_count < Q:
	question = list(map(int,input().split()))
	if len(question) == 1:
		A = np.sort(A)
		len_A = len(A)
		if len_A % 2 != 0:
			X = np.zeros_like(A)
			ans_1.append(A[len_A // 2])
			X = np.abs(ans_1 - A)
			ans_2.append(np.sum(X) + sum_B)
		else:
			X = np.zeros_like(A)
			ans_1.append(A[(len_A // 2) - 1])
			X = np.abs(ans_1 - A)
			ans_2.append(np.sum(X) + sum_B)		
	
		question_count += 1
			
	else:
		A = np.append(A,question[1])
		sum_B += question[2]
		question_count += 1
		
for i in range(len(ans_1)):
	print(ans_1[i],ans_2[i])

Submission status: Runtime Error

Solution code:
from bisect import bisect_left

if __name__ == '__main__':
    q = int(input())
    query = [list(map(int, input().split())) for _ in range(q)]

    first_query = query[0]
    [_, a, b] = first_query
    a_list = [a]
    b_sum = b
    state = [a]
    state_num = [1]
    center_index = 0
    mode = 0

    for que in query[1:]:
        # print(center_index)
        # print(state)
        # print(a_list)
        # print(b_sum)
        # print()
        if que[0] == 1:
            if mode == 0:
                insert_index = bisect_left(a_list, que[1])
                if center_index >= insert_index:
                    state = [que[1], state[0]]
                    center_index = insert_index
                else:
                    state = [state[0], que[1]]
                    # center_index = center_index
                mode = 1
            else:
                insert_index = bisect_left(a_list, que[1])
                if center_index + 1 == insert_index:
                    state = [que[1]]
                elif center_index >= insert_index:
                    state = [state[0]]
                else:
                    state = [state[1]]
                center_index = center_index + 1
                mode = 0
            a_list.insert(insert_index, que[1])
            b_sum = b_sum + que[2]
        else:
            min_x = state[0]
            for a in a_list:
                min_val = min_val + abs(min_x - a)
            print(min_x, min_val + b_sum)

# 状態の更新
# 中央がある（中央）
# 　中央より左　→　中央がない（中央の１つ左　中央）
# 　中央より右　→　中央がない（中央　中央の１つ右）
# 中央がない（中央左　中央右）
# 　中央左より左　→　中央がある（中央左）
# 　中央左と中央右の間　→　中央がある（新しい値）
# 　中央右より右　→　中央がある（中央右）

# 求値
# 　中央、または、中央左がXとなる
# 　XとAと（Bの和）から最小値を計算する

# |x - a|
# x - a  x-a >= 0
# a - x  x-a < 0
# center_indexの部分で切り替わる

Submission status: Runtime Error

Solution code:
import numpy as np

num_q = int(input())
x = 0
f = 0
a = []
c = False
b = 0

for b in range(num_q):
  q = input()
  if q[0] == '2':
    print(x, f)
  else:
    _, n, m = map(int, q.split(" "))
    a.append(n)
    a.sort()
    x_tmp = a[b//2]
    if c:
      f += m + np.abs(x - n)
    else:
      f += m + np.abs(x_tmp - n)
    c = ~c
    x = x_tmp
    b += 1

Submission status: Runtime Error

Solution code:
import bisect

Q = int(input())

Query, A, B = [], [], 0
for _ in range(Q):
    q = tuple(map(int, input().split()))
    Query.append(q)
    if len(q) == 3:
        A.append(q[1])
        B += q[2]

A.sort()

lA = len(A)
if lA % 2 == 0:
    mi = lA // 2 - 1
    l, m, r = sum(A[lA // 2:]), 0, sum(A[:lA // 2])
    even = True
else:
    mi = lA // 2
    l, m, r = sum(A[lA // 2 + 1:]), A[A // 2], sum(A[:lA // 2])
    even = False

ans = []
for i in range(Q):
    q = Query[Q - i - 1]
    if len(q) == 3:
        ix = bisect.bisect_left(A, q[1])
        if even:
            if ix > mi:
                l, m, r = l, A[mi], r - A[ix]
                mi = mi
            else:
                l, m, r = l - A[ix], A[mi + 1], r
                mi = mi - 1
            even = False
        else:
            if ix > mi:
                l, m, r = l, 0, r - A[ix]
                mi = mi
            else:
                l, m, r = l - A[ix], 0, r
                mi = mi - 1
            even = True
        B -= q[2]
    else:
        ans.append((mi, l + r + B))

la = len(ans)
for i in range(la):
    print(ans[la - i - 1][0], ans[la - i - 1][1])

Submission status: Runtime Error

Solution code:
import numpy as np
Q = int(input())
question_count = 1
z = []
A = np.array(z)
ans_1 = []
ans_2 = []
sum_B = 0
while question_count < Q:
	question = list(map(int,input().split()))
	if len(question) == 1:
		A = np.sort(A)
		len_A = len(A)
		if len_A % 2 != 0:
			X = np.zeros_like(A)
			ans_1.append(A[len_A // 2])
			X = np.abs(ans_1 - A)
			ans_2.append(np.sum(X) + sum_B)
		else:
			X = np.zeros_like(A)
			ans_1.append(A[(len_A // 2) - 1])
			X = np.abs(ans_1 - A)
			ans_2.append(np.sum(X) + sum_B)		
	
		question_count += 1
			
	else:
		A = np.append(A,question[1])
		sum_B += question[2]
		question_count += 1
		
for i in range(Q):
	print(ans_1[i],ans_2[i])

Submission status: Runtime Error

Solution code:
import heapq
import copy
import math

Q = int(input())
A = []
B = 0
for _ in range(Q):
    nums = [int(i) for i in input().split()]
    if nums[0] == 1:
        heapq.heappush(A, nums[1])
        B += nums[2]
    else:
        copy = copy.deepcopy(A)
        copyA = [heapq.heappop(copy) for i in range(len(A))]
        x = copyA[int(math.floor((len(A) - 1) / 2))]
        res = sum([abs(x - i) for i in A]) + B
        print(x, res)

Submission status: Runtime Error

Solution code:
import numpy as np

num_q = int(input())
x = 0
f = 0
a = []
c = False

for i in range(num_q):
  q = input()
  if q[0] == '2':
    print(x, f)
  else:
    _, n, m = map(int, q.split(" "))
    a.append(n)
    a.sort()
    x_tmp = a[(i+1)//2]
    if c:
      f += m + np.abs(x - n)
    else:
      f += m + np.abs(x_tmp - n)
    c = ~c
    x = x_tmp

Submission status: Runtime Error

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 = length//2
        return sum(a[1+center:]) - sum(a[:center])
    else:
        center = 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:
        if len(ab) %2 == 0:
            ab.sort()
            center = 0
            ss = ab_sum(ab, center)
            
            print(ab[center] ,b + ss)
    else:
        b += a[2]
        ab.append(a[1])

Submission status: Runtime Error

Solution code:
from bisect import bisect_left as bl
d=[]
c,r=0,0
for i in range(int(input())):
    s=input()
    if s[0]=="1":
        e,f=int(s[2]),int(s[4])
        c+=f
        d.insert(bl(d,e),e)
        r+=1
    else:
        if r%2==0:
            s=d[r//2-1]+d[r//2]/2
            p=sum(d[r//2:])-sum(d[:r//2])
        else:
            s=d[r//2]
            p=sum(d[r//2+1:])-sum(d[:r//2])
        print(s,p+c)