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 io::*;

pub mod io {
    use std;
    use std::str::FromStr;

    pub struct Scanner<'a> {
        iter: std::str::SplitWhitespace<'a>,
    }

    impl<'a> Scanner<'a> {
        pub fn new(s: &'a str) -> Scanner<'a> {
            Scanner {
                iter: s.split_whitespace(),
            }
        }

        pub fn next<T: FromStr>(&mut self) -> T {
            let s = self.iter.next().unwrap();
            if let Ok(v) = s.parse::<T>() {
                v
            } else {
                panic!("Parse error")
            }
        }

        pub fn next_vec_len<T: FromStr>(&mut self) -> Vec<T> {
            let n: usize = self.next();
            self.next_vec(n)
        }

        pub fn next_vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
            (0..n).map(|_| self.next()).collect()
        }

        pub fn next_mat<T: FromStr>(&mut self, w: usize, h: usize) -> Vec<Vec<T>> {
            (0..h).map(|_| self.next_vec(w)).collect()
        }

        pub fn next_vec_char(&mut self) -> Vec<char> {
            self.next::<String>().chars().collect()
        }

        pub fn next_mat_char(&mut self, n: usize) -> Vec<Vec<char>> {
            (0..n).map(|_| self.next_vec_char()).collect()
        }
    }

    pub fn read_string() -> String {
        use std::io::Read;

        let mut s = String::new();
        std::io::stdin().read_to_string(&mut s).unwrap();
        s
    }

    pub fn read_line() -> String {
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).unwrap();
        s.trim_right().to_owned()
    }
}

use std::collections::{BTreeSet, BTreeMap};
use std::iter::FromIterator;

fn main() {
    let input = read_string();
    let mut sc = Scanner::new(&input);
    let q: usize = sc.next();
    let mut qs: Vec<(usize, i64, i64)> = Vec::new();
    let mut set: BTreeSet<i64> = BTreeSet::new();
    set.insert(i64::min_value());
    for i in 0..q {
        let t: usize = sc.next();
        if t == 1 {
            let a: i64 = sc.next();
            let b: i64 = sc.next();
            set.insert(a);
            qs.push((t, a, b));
        } else {
            qs.push((t,0,0));
        }
    }
    let map: BTreeMap<i64, usize> = BTreeMap::from_iter(set.iter().cloned().zip(0..));
    let av: Vec<i64> = set.iter().cloned().collect();
    let mut bitn = BIT::new(av.len());
    let mut bitv = BIT::new(av.len());
    let mut b: i64 = 0;
    let mut an = 0;
    for i in 0..q {
        if qs[i].0 == 1 {
            let ai = *map.get(&qs[i].1).unwrap();
            b += qs[i].2;
            bitn.add(ai, 1);
            bitv.add(ai, qs[i].1);
            an += 1;
        } else {
            let mut l = 0;
            let mut r = av.len();
            while l + 1 < r {
                let m = (l + r) / 2;
                if bitn.sum(m) < (an + 1) / 2 {
                    l = m;
                } else {
                    r = m;
                }
            }
            let x = av[r];
            let mut m = bitv.sum(av.len() - 1) - bitv.sum(r) - (bitn.sum(av.len() - 1) - bitn.sum(r) - bitn.sum(r - 1)) * x - bitv.sum(r - 1) + b;
            println!("{} {}", x, m);
        }
    }
}

struct BIT {
    v: Vec<i64>,
    n: usize
}

impl BIT {
    fn new(n: usize) -> BIT {
        BIT{v: vec![0; n + 1], n: n}
    }

    // add x to i th element
    fn add(&mut self, i: usize, x: i64) {
        let mut id = i as i64 + 1;
        while id <= self.n as i64 {
            self.v[id as usize] += x;
            id += id & -id;
        }
    }

    // sum up to i th element
    fn sum(&self, i: usize) -> i64 {
        let mut id = i + 1;
        let mut ret = 0;
        while id > 0 {
            ret += self.v[id];
            id &= id - 1;
        }
        ret
    }
}