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)

use std::collections::BTreeMap;
use std::io::*;
use std::str::FromStr;

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn insert(ps: &mut Vec<BTreeMap<i64, usize>>, offsets: &mut Vec<i64>, a: i64, cnt: i64, lr: usize) {
    let la = -*ps[0].iter().next().unwrap().0;
    let ra = *ps[1].iter().next().unwrap().0;
    if lr == 0 {
        let entry = ps[0].entry(-a).or_insert(0);
        *entry += 1;
        if a <= la {
            offsets[0] += (la - a).abs();
        } else {
            offsets[0] += (a - la).abs() * cnt / 2;
        }
    } else {
        let entry = ps[1].entry(a).or_insert(0);
        *entry += 1;
        if ra <= a {
            offsets[1] += (a - ra).abs();
        } else {
            offsets[1] += (ra - a).abs() * (cnt - 1) / 2;
        }
    }
}

fn push(ps: &mut Vec<BTreeMap<i64, usize>>, offsets: &mut Vec<i64>, a: i64, cnt: i64) {
    if cnt == 0 {
        ps[0].insert(-a, 1);
        return;
    }
    let la = -*ps[0].iter().next().unwrap().0;
    if cnt == 1 {
        if a < la {
            ps[1].insert(la, 1);
            ps[0].clear();
            ps[0].insert(-a, 1);
        } else {
            ps[1].insert(a, 1);
        }
        return;
    }
    let ra = *ps[1].iter().next().unwrap().0;
    if a <= ra {
        insert(ps, offsets, a, cnt, 0);
        let la = -*ps[0].iter().next().unwrap().0;
        if cnt % 2 == 1 {
            let mut delete_flag = false;
            {
                let entry = ps[0].entry(-la).or_insert(0);
                *entry -= 1;
                if *entry == 0 {
                    delete_flag = true;
                }
            }
            if delete_flag {
                ps[0].remove(&-la);
                let nla = -ps[0].iter().next().unwrap().0;
                offsets[0] -= (la - nla).abs() * cnt / 2;
            }
            insert(ps, offsets, la, cnt, 1);
        }
    } else {
        insert(ps, offsets, a, cnt, 1);
        let ra = *ps[1].iter().next().unwrap().0;
        if cnt % 2 == 0 {
            let mut delete_flag = false;
            {
                let entry = ps[1].entry(ra).or_insert(0);
                *entry -= 1;
                if *entry == 0 {
                    delete_flag = true;
                }
            }
            if delete_flag {
                ps[1].remove(&ra);
                let nra = ps[1].iter().next().unwrap().0;
                offsets[1] -= (ra - nra).abs() * (cnt - 1) / 2;
            }
            insert(ps, offsets, ra, cnt, 0);
        }
    }
}

fn main() {
    let q = read::<usize>();
    let mut cnt: i64 = 0;
    let mut b_sum = 0;
    let mut ps = vec![BTreeMap::<i64, usize>::new(); 2];
    let mut offsets = vec![0; 2];
    for _i in 0..q {
        let t = read::<usize>();
        if t == 1 {
            let a = read::<i64>();
            let b = read::<i64>();
            b_sum += b;
            push(&mut ps, &mut offsets, a, cnt);
            cnt += 1;
        } else {
            let lpos = if cnt == 0 {
                0
            } else {
                -*ps[0].iter().next().unwrap().0
            };
            let rpos = if cnt <= 1 {
                lpos
            } else {
                *ps[1].iter().next().unwrap().0
            };
            let ans = offsets[0] + offsets[1] + cnt / 2 * (rpos - lpos) + b_sum;
            println!("{} {}", lpos, ans);
        }
    }
}