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: Wrong Answer)

/*------------------------------------------------------------------*/
/*------------------------- begin template -------------------------*/
/*------------------------------------------------------------------*/

// ref: tanakh <https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8>
// diff: using Parser
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut parser = Parser::from_str($s);
        input_inner!{parser, $($r)*}
    };
    (parser = $parser:ident, $($r:tt)*) => {
        input_inner!{$parser, $($r)*}
    };
    (new_stdin_parser = $parser:ident, $($r:tt)*) => {
        let stdin = std::io::stdin();
        let reader = std::io::BufReader::new(stdin.lock());
        let mut $parser = Parser::new(reader);
        input_inner!{$parser, $($r)*}
    };
    ($($r:tt)*) => {
        input!{new_stdin_parser = parser, $($r)*}
    };
}

macro_rules! input_inner {
    ($parser:ident) => {};
    ($parser:ident, ) => {};
    ($parser:ident, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($parser, $t);
        input_inner!{$parser $($r)*}
    };
}

macro_rules! read_value {
    ($parser:ident, ( $($t:tt),* )) => {
        ( $(read_value!($parser, $t)),* )
    };
    ($parser:ident, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($parser, $t)).collect::<Vec<_>>()
    };
    ($parser:ident, chars) => {
        read_value!($parser, String).chars().collect::<Vec<char>>()
    };
    ($parser:ident, char_) => {
        read_value!($parser, String).chars().collect::<Vec<char>>()[0]
    };
    ($parser:ident, usize1) => {
        read_value!($parser, usize) - 1
    };
    ($parser:ident, i64_) => {
        $parser.fast_i64()
    };
    ($parser:ident, usize_) => {
        $parser.fast_i64() as usize
    };
    ($parser:ident, usize1_) => {
        ($parser.fast_i64() - 1) as usize
    };
    ($parser:ident, $t:ty) => {
        $parser.next::<$t>().expect("Parse error")
    };
}

use std::io;
use std::io::BufRead;
use std::str;

// ref: tatsuya6502 <https://qiita.com/tatsuya6502/items/cd448486f7ef7b5b8c7e>
// ref: wariuni <https://qiita.com/tatsuya6502/items/cd448486f7ef7b5b8c7e#comment-7040a5ae96305e884eb9>
// diff: using std::io::BufRead::fill_buf()
pub struct Parser<R> {
    pub reader: R,
    buf: Vec<u8>,
    pos: usize,
}

impl Parser<io::Empty> {
    pub fn from_str(s: &str) -> Parser<io::Empty> {
        Parser {
            reader: io::empty(),
            buf: s.as_bytes().to_vec(),
            pos: 0,
        }
    }
}

impl<R: BufRead> Parser<R> {
    pub fn new(reader: R) -> Parser<R> {
        Parser {
            reader: reader,
            buf: vec![],
            pos: 0,
        }
    }
    pub fn update_buf(&mut self) {
        self.buf.clear();
        self.pos = 0;
        loop {
            let (len, complete) = {
                let buf2 = self.reader.fill_buf().unwrap();
                self.buf.extend_from_slice(buf2);
                let len = buf2.len();
                (len, buf2[len - 1] <= 0x20)
            };
            self.reader.consume(len);
            if complete {
                break;
            }
        }
    }
    pub fn next<T: str::FromStr>(&mut self) -> Result<T, T::Err> {
        loop {
            let mut begin = self.pos;
            while begin < self.buf.len() && (self.buf[begin] <= 0x20) {
                begin += 1;
            }
            let mut end = begin;
            while end < self.buf.len() && (self.buf[end] > 0x20) {
                end += 1;
            }
            if begin != self.buf.len() {
                self.pos = end;
                return unsafe { str::from_utf8_unchecked(&self.buf[begin..end]) }.parse::<T>();
            } else {
                self.update_buf();
            }
        }
    }
    pub fn fast_i64(&mut self) -> i64 {
        loop {
            let mut begin = self.pos;
            while begin < self.buf.len() && (self.buf[begin] <= 0x20) {
                begin += 1;
            }
            if begin == self.buf.len() {
                self.update_buf();
                continue;
            }
            let mut res = 0;
            let (is_positive, mut end) = match self.buf[begin] {
                b'+' => (true, begin + 1),
                b'-' => (false, begin + 1),
                _ => (true, begin),
            };
            while end < self.buf.len() && (self.buf[end] > 0x20) {
                res = res * 10 + (self.buf[end] as i64 - '0' as i64);
                end += 1;
            }
            if begin != self.buf.len() {
                self.pos = end;
                return if is_positive { res } else { -res };
            } else {
                self.update_buf();
            }
        }
    }
}

#[allow(unused_imports)]
use std::cmp::*;

fn main() {
    input! {
        new_stdin_parser = parser,
    }
    let res = solve(&mut parser);
    print!("{}", res);
}

/*------------------------------------------------------------------*/
/*-------------------------- end template --------------------------*/
/*------------------------------------------------------------------*/

use std::rc::Rc;

pub enum Trie {
    None,
    Leaf {
        x: u32,
    },
    Node {
        n: usize,
        left: Rc<Trie>,
        right: Rc<Trie>,
    },
}

impl Trie {
    pub fn new() -> Rc<Trie> {
        Rc::new(Trie::None)
    }
    pub fn insert(trie: &Rc<Trie>, x: u32) -> Rc<Trie> {
        Trie::insert_sub(trie, x, 31)
    }
    pub fn len(&self) -> usize {
        match self {
            &Trie::None => 0,
            &Trie::Leaf { x: _ } => 1,
            &Trie::Node {
                n: n,
                left: _,
                right: _,
            } => n,
        }
    }
    pub fn insert_sub(trie: &Rc<Trie>, x: u32, i: usize) -> Rc<Trie> {
        if i == 0 {
            Rc::new(Trie::Leaf { x: x })
        } else {
            if x & (1 << i) != 0 {
                let (left, right) = match trie.as_ref() {
                    &Trie::None => (
                        Rc::new(Trie::None),
                        Trie::insert_sub(&Rc::new(Trie::None), x, i - 1),
                    ),
                    &Trie::Leaf { x: _ } => {
                        panic!();
                    }
                    &Trie::Node {
                        n: _,
                        left: ref left,
                        right: ref right,
                    } => (left.clone(), Trie::insert_sub(&right, x, i - 1)),
                };
                Rc::new(Trie::Node {
                    n: left.len() + right.len(),
                    left: left,
                    right: right,
                })
            } else {
                let (left, right) = match trie.as_ref() {
                    &Trie::None => (
                        Trie::insert_sub(&Rc::new(Trie::None), x, i - 1),
                        Rc::new(Trie::None),
                    ),
                    &Trie::Leaf { x: _ } => {
                        panic!();
                    }
                    &Trie::Node {
                        n: _,
                        left: ref left,
                        right: ref right,
                    } => (Trie::insert_sub(&left, x, i - 1), right.clone()),
                };
                Rc::new(Trie::Node {
                    n: left.len() + right.len(),
                    left: left,
                    right: right,
                })
            }
        }
    }
    pub fn i(trie: &Rc<Trie>, i: usize) -> u32 {
        assert!(i < trie.len());
        match trie.as_ref() {
            &Trie::None => {
                panic!();
            }
            &Trie::Leaf { x: x } => {
                if i == 0 {
                    x
                } else {
                    panic!();
                }
            }
            &Trie::Node {
                n: _,
                left: ref left,
                right: ref right,
            } => {
                if i < left.len() {
                    Trie::i(left, i)
                } else {
                    Trie::i(right, i - left.len())
                }
            }
        }
    }
    pub fn x_to_i(trie: &Rc<Trie>, t: u32, i: usize) -> usize {
        match trie.as_ref() {
            &Trie::None => {
                panic!();
            }
            &Trie::Leaf { x: x } => {
                if i == 0 && x == t {
                    0
                } else {
                    panic!();
                }
            }
            &Trie::Node {
                n: _,
                left: ref left,
                right: ref right,
            } => {
                if t & (1 << i) != 0 {
                    left.len() + Trie::x_to_i(right, t, i - 1)
                } else {
                    Trie::x_to_i(left, t, i - 1)
                }
            }
        }
    }
    pub fn to_vec(trie: &Rc<Trie>, xs: &mut Vec<u32>) {
        match trie.as_ref() {
            &Trie::None => {}
            &Trie::Leaf { x: x } => {
                xs.push(x);
            }
            &Trie::Node {
                n: _,
                left: ref left,
                right: ref right,
            } => {
                Trie::to_vec(&left, xs);
                Trie::to_vec(&right, xs);
            }
        }
    }
}

use std::fmt::Write;
fn solve<R: BufRead>(parser: &mut Parser<R>) -> String {
    let mut buf = String::new();
    input! {
        parser = parser,
        q: usize,
    }
    let inf = 1i64 << 50;
    let mut base = 0;
    let mut tree = Trie::new();
    let mut center_x = 0;
    let mut dd = 0;
    for qi in 0..q {
        // eprintln!("qi={}", qi);
        input! {
            parser = parser,
            t: usize,
        }
        if t == 1 {
            input! {
                parser = parser,
                a: i64,
                b: i64,
            }
            let a = (a + 1_000_000_000) as u32;
            // eprintln!("insert {}", a);
            tree = Trie::insert(&tree, a);
            let mut ts = vec![];
            Trie::to_vec(&tree, &mut ts);
            // eprintln!("tree = {:?}", ts);
            base += b;
            if qi == 0 {
                center_x = a;
            } else {
                // eprintln!("center_x = {}", center_x);
                let prev_center_i = Trie::x_to_i(&tree, center_x, 31);
                let center_i = (tree.len() - 1) / 2;
                // eprintln!("dd_1 = {}", dd);
                if center_x <= a {
                    dd += a as i64 - center_x as i64;
                } else {
                    dd += center_x as i64 - a as i64;
                }
                // eprintln!("dd_2 = {}", dd);
                if center_i < prev_center_i {
                    // eprintln!("a");
                    let new_center = Trie::i(&tree, center_i);
                    dd += (tree.len() - center_i) as i64 * (center_x as i64 - new_center as i64);
                    dd -= tree.len() as i64 * (center_x as i64 - new_center as i64);
                    center_x = new_center;
                } else if prev_center_i < center_i {
                    // eprintln!("b");
                    let new_center = Trie::i(&tree, center_i);
                    dd -=
                        (tree.len() - center_i - 1) as i64 * (new_center as i64 - center_x as i64);
                    dd += tree.len() as i64 * (new_center as i64 - center_x as i64);
                    center_x = new_center;
                }
                // eprintln!("dd_3 = {}", dd);
            }
        } else if t == 2 {
            let i = (tree.len() - 1) / 2;
            let t = Trie::i(&tree, i);
            writeln!(buf, "{} {}", t as i64 - 1_000_000_000, base + dd);
        }
    }

    buf
}