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

Solution in Rust (1.15.1) (judge verdict: Accepted)

use std::collections::BinaryHeap;
use std::io::{self, BufRead};

#[derive(Debug, Copy, Clone)]
enum Query {
    Update(i64, i64),
    Calc,
}

fn main() {
    let stdin = io::stdin();
    let mut handle = stdin.lock();
    let mut buf = String::new();
    handle.read_line(&mut buf).unwrap();
    let Q = buf.trim().parse::<usize>().unwrap();
    buf.clear();

    let mut queries = Vec::with_capacity(Q);
    for _ in 0..Q {
        handle.read_line(&mut buf);
        {
            let mut it = buf
                .trim()
                .split_whitespace()
                .map(|x| x.parse::<i64>().unwrap());
            if it.next().unwrap() == 1 {
                let a = it.next().unwrap();
                let b = it.next().unwrap();
                queries.push(Query::Update(a, b));
            } else {
                queries.push(Query::Calc);
            }
        }
        buf.clear();
    }

    let mut b_sum = 0;
    let mut first_half_maxheap: BinaryHeap<i64> = BinaryHeap::new();
    let mut latter_half_minheap: BinaryHeap<i64> = BinaryHeap::new();
    let mut first_half_sum = 0;
    let mut latter_half_sum = 0;

    for q in queries {
        match q {
            Query::Calc => {
                let x = first_half_maxheap.peek().unwrap();
                //dbg!(
                //&x,
                //first_half_maxheap.len(),
                //first_half_sum,
                //latter_half_minheap.len(),
                //latter_half_sum
                //);
                let ans = (x * first_half_maxheap.len() as i64 - first_half_sum)
                    + (-x * latter_half_minheap.len() as i64 + latter_half_sum)
                    + b_sum;
                println!("{} {}", x, ans);
            }
            Query::Update(a, b) => {
                b_sum += b;
                if first_half_maxheap.is_empty() {
                    first_half_maxheap.push(a);
                    first_half_sum += a;
                } else if first_half_maxheap.len() > latter_half_minheap.len() {
                    let &t = first_half_maxheap.peek().unwrap();
                    if a >= t {
                        latter_half_minheap.push(-a);
                        latter_half_sum += a;
                    } else {
                        first_half_maxheap.pop();
                        latter_half_minheap.push(-t);
                        latter_half_sum += t;
                        first_half_maxheap.push(a);
                        first_half_sum -= t;
                        first_half_sum += a;
                    }
                } else {
                    let t = -latter_half_minheap.peek().unwrap();
                    if a <= t {
                        first_half_maxheap.push(a);
                        first_half_sum += a;
                    } else {
                        latter_half_minheap.pop();
                        latter_half_sum -= t;
                        latter_half_sum += a;
                        latter_half_minheap.push(-a);
                        first_half_maxheap.push(t);
                        first_half_sum += t;
                    }
                }
            }
        }
    }
}