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 C (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>

#define KI_MAX (1 << 18) /* 262144 */

int count_ki[KI_MAX * 2 - 1];
int64_t sum_ki[KI_MAX * 2 - 1];

void add_count(int idx, int delta) {
	int pos = (KI_MAX - 1) + idx;
	count_ki[pos] += delta;
	while (pos > 0) {
		pos = (pos - 1) / 2;
		count_ki[pos] += delta;
	}
}

int get_count_i(int idx, int qmin, int qmax, int smin, int smax) {
	if (qmax <= smin || smax <= qmin) { /* クエリがセグメントから完全に外れている */
		return 0;
	} else if (qmin <= smin && smax <= qmax) { /* セグメントがクエリに完全に含まれる */
		return count_ki[idx];
	} else {
		int smid = smin + (smax - smin) / 2;
		int left = get_count_i(idx * 2 + 1, qmin, qmax, smin, smid);
		int right = get_count_i(idx * 2 + 2, qmin, qmax, smid, smax);
		return left + right;
	}
}

int get_count(int min, int max) {
	return min >= max ? 0 : get_count_i(0, min, max, 0, KI_MAX);
}

void add_sum(int idx, int64_t delta) {
	int pos = (KI_MAX - 1) + idx;
	sum_ki[pos] += delta;
	while (pos > 0) {
		pos = (pos - 1) / 2;
		sum_ki[pos] += delta;
	}
}

int64_t get_sum_i(int idx, int qmin, int qmax, int smin, int smax) {
	if (qmax <= smin || smax <= qmin) { /* クエリがセグメントから完全に外れている */
		return 0;
	} else if (qmin <= smin && smax <= qmax) { /* セグメントがクエリに完全に含まれる */
		return sum_ki[idx];
	} else {
		int smid = smin + (smax - smin) / 2;
		int64_t left = get_sum_i(idx * 2 + 1, qmin, qmax, smin, smid);
		int64_t right = get_sum_i(idx * 2 + 2, qmin, qmax, smid, smax);
		return left + right;
	}
}

int64_t get_sum(int min, int max) {
	return min >= max ? 0 : get_sum_i(0, min, max, 0, KI_MAX);
}

int hikaku(const void* x, const void* y) {
	int a = *(const int*)x, b = *(const int*)y;
	return a < b ? -1 : a > b;
}

int Q;
int query[271828], a[271828], b[271828];

int za_count, za_count_beta;
int za[777777];

int za_get(int taisyou) {
	int l = 0, r = za_count - 1;
	while (l <= r) {
		int m = l + (r - l) / 2;
		if (za[m] == taisyou) return m;
		else if (za[m] < taisyou) l = m + 1;
		else r = m - 1;
	}
	printf("ERROR: %d not found!!!!!\n", taisyou);
	exit(2);
}

int main(void) {
	int i;
	int64_t b_sum = 0;
	if (scanf("%d", &Q) != 1) return 1;
	za_count_beta = 0;
	for (i = 0; i < Q; i++) {
		if (scanf("%d", &query[i]) != 1) return 1;
		if (query[i] == 1) {
			if (scanf("%d%d", &a[i], &b[i]) != 2) return 1;
			za[za_count_beta++] = a[i];
		} else if (query[i] == 2) {
			/* nop */
		} else {
			puts("INVALID QUERY");
			return 2;
		}
	}
	qsort(za, za_count_beta, sizeof(*za), hikaku);
	za_count = 1;
	for (i = 1; i < za_count_beta; i++) {
		if (za[za_count - 1] != za[i]) {
			za[za_count++] = za[i];
		}
	}

	for (i = 0; i < Q; i++) {
		if (query[i] == 1) {
			int idx = za_get(a[i]);
			add_count(idx, 1);
			add_sum(idx, a[i]);
			b_sum += b[i];
		} else {
			int all = get_count(0, za_count);
			int target = (all + 1) / 2;
			int count_left, count_right;
			int64_t sum_left, sum_right;
			int less = -1, ge = za_count - 1;
			while (less + 1 < ge) {
				int mid = less + (ge - less) / 2;
				if (get_count(0, mid + 1) < target) less = mid; else ge = mid;
			}
			count_left = get_count(0, ge);
			count_right = get_count(ge + 1, za_count);
			sum_left = get_sum(0, ge);
			sum_right = get_sum(ge + 1, za_count);
			printf("%d %" PRId64 "\n", za[ge],
				(int64_t)za[ge] * count_left - sum_left +
				sum_right - (int64_t)za[ge] * count_right +
				b_sum);
		}
	}

	return 0;
}

/*

左右に半分ずつに分かれる位置で最小になる
bは単純に足していく

*/