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)

#![allow(unused_imports, unused_macros, dead_code)]

macro_rules! min {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| min!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = min!($($ys),*);
        if $x < t { $x } else { t }
    }}
}
macro_rules! max {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| max!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = max!($($ys),*);
        if $x > t { $x } else { t }
    }}
}
macro_rules! ewriteln {
    ($($args:expr),*) => { let _ = writeln!(&mut std::io::stderr(), $($args),*); };
}
macro_rules! trace {
    ($x:expr) => { ewriteln!(">>> {} = {:?}", stringify!($x), $x) };
    ($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(x) = it.next() { print!("{}", x); }
        for x in it { print!(" {}", x); }
        println!("");
    }};
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}

const M: i64 = 1_000_000_007;

#[derive(Debug)]
struct MedianHeap {
    head: std::collections::BinaryHeap<i64>,
    tail: std::collections::BinaryHeap<i64>,
    sum_head: i64,
    sum_tail: i64
}
#[derive(Debug, PartialEq, Eq)]
enum Median {
    None,
    Just(i64),
    Between(i64, i64),
}
impl MedianHeap {
    fn new() -> MedianHeap {
        MedianHeap {
            head: std::collections::BinaryHeap::new(),
            tail: std::collections::BinaryHeap::new(),
            sum_head: 0,
            sum_tail: 0,
        }
    }
    fn len(&self) -> usize {
        (self.head.len() + self.tail.len()) / 2
    }
    fn push(&mut self, x: i64) {
        match (self.head.peek(), self.tail.peek()) {
            (None, None) => {
                self.head.push(x);
                self.tail.push(-x);
                self.sum_head = x;
                self.sum_tail = x;
            },
            (Some(&a), Some(&b)) => {
                let b = -b;
                if a <= x && x <= b {
                    self.head.push(x);
                    self.tail.push(-x);
                    self.sum_head += x;
                    self.sum_tail += x;
                } else if x < a {
                    self.head.push(x);
                    self.head.push(x);
                    let _ = self.head.pop();
                    self.tail.push(-a);
                    self.sum_head += x + x;
                    self.sum_head -= a;
                    self.sum_tail += a;
                } else {
                    self.tail.push(-x);
                    self.tail.push(-x);
                    let _ = self.tail.pop();
                    self.head.push(b);
                    self.sum_tail += x + x;
                    self.sum_tail -= b;
                    self.sum_head += b;
                }
            },
            _ => {}
        }
    }
    fn peek(&self) -> Median {
        let n = self.len();
        if n == 0 {
            Median::None
        } else if n % 2 == 1 {
            Median::Just(*self.head.peek().unwrap())
        } else {
            let a = self.head.peek().unwrap();
            let b = self.tail.peek().unwrap();
            Median::Between(*a, -b)
        }
    }
}

fn main() {
    let mut sc = Scanner::new();
    let mut med = MedianHeap::new();
    let n: usize = sc.cin();
    let mut base = 0;
    for _ in 0..n {
        let ty: usize = sc.cin();
        if ty == 1 {
            let a: i64 = sc.cin();
            let b: i64 = sc.cin();
            base += b;
            med.push(a);
        } else {

            match med.peek() {
                Median::None => {},
                Median::Just(x) => {
                    // trace!(&med.head, &med.tail);
                    // trace!(&med.sum_head, &med.sum_tail);
                    let left_sum = (med.head.len() as i64 - 1) / 2 * x - (med.sum_head - x) / 2;
                    let right_sum = (med.sum_tail - x) / 2 - (med.tail.len() as i64 - 1) / 2 * x;
                    // trace!(left_sum, right_sum);
                    put!(x, base + left_sum + right_sum);
                },
                Median::Between(x, _) => {
                    // trace!(&med.head, &med.tail);
                    // trace!(&med.sum_head, &med.sum_tail);
                    let left_sum = (med.head.len() as i64) / 2 * x - med.sum_head / 2;
                    let right_sum = med.sum_tail / 2 - (med.tail.len() as i64) / 2 * x;
                    // trace!(left_sum, right_sum);
                    put!(x, base + left_sum + right_sum);
                }
            }

        }
    }
}

use std::io::{self, Write};
use std::str::FromStr;
use std::collections::VecDeque;

struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
impl Scanner {
    fn new() -> Self { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
    fn chars(&mut self) -> Vec<char> { self.cin::<String>().chars().collect() }
    fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T> { (0..n).map(|_| self.cin()).collect() }
}