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

#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <bitset>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <string>
#include <chrono>
#include <random>
#include <cmath>
#include <cassert>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <sstream>

using namespace std;

class BIT {
    public:
        BIT(int n) : N(n), data(n, 0) {

        }

        void put(int x, long long v) {
            for (; x < N; x |= x + 1) {
                data[x] += v;
            }
        }

        long long get(int x) {
            long long res = 0;
            for (; x >= 0; x = (x & (x + 1)) - 1) {
                res += data[x];
            }
            return res;
        }

        int kthElement(long long K) {
            int res = 0;
            int h = 0;
            while (1 << (h + 1) <= N) {
                ++h;
            }

            for (int i = h; i >= 0; --i) {
                if (res + (1 << i) <= N) {
                    if (data[res + (1 << i) - 1] < K) {
                        K -= data[res + (1 << i) - 1];
                        res |= 1 << i;
                    }
                }
            }
            return res;
        }

    private:
        int N;
        vector<long long> data;
};

class Solution {
public:
    vector<pair<long long, long long>> solve(vector<pair<int,pair<long long, long long>>>& queries) {
        vector<long long> X;
        for (auto& q : queries) {
            if (q.first == 1) {
                long long a = q.second.first;
                long long b = q.second.second;
                X.push_back(a);
            }
        }

        sort(X.begin(), X.end());
        X.erase(unique(X.begin(), X.end()), X.end());
        auto index = [&](long long x) {
            return lower_bound(X.begin(), X.end(), x) - X.begin();
        };

        BIT bitCnt(X.size() + 123);
        BIT bitSum(X.size() + 123);

        vector<pair<long long, long long>> res;
        long long N = 0;
        long long B = 0;
        long long A = 0;
        for (auto& q :queries) {
            if (q.first == 1) {
                long long a = q.second.first;
                long long b = q.second.second;
                int x = index(a);
                bitCnt.put(x, 1);
                bitSum.put(x, a);
                A += a;
                B += b;
                ++N;
            } else {
                int l = (N - 1) / 2 + 1;
                int r = N - l;
                long long m1 = bitCnt.kthElement(l);
                // long long m2 = m1;
                // if ((N & 1) == 0) {
                //     m2 = bitCnt.kthElement(N / 2 + 1);
                // }
                long long L = bitSum.get(m1 - 1);
                long long cnt = bitCnt.get(m1 - 1);
                assert(cnt < l);
                assert(A == bitSum.get(X.size()));

                L += (l - cnt) * X[m1];

                // long long ans = X[m1] * l - L;
                // ans += A - L - X[m1] * r;
                
                long long ans = A - L - L + ((N & 1) == 1 ? X[m1] : 0);
                ans += B;
                res.emplace_back(X[m1], ans);

                // cout << N << " " << m1 << " " << m2 << " " <<  l << " " << r << " " << L << endl;

            }
        }
        return res;
    }

private:

};

int main(int argc, char** argv) {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int q;
    cin >> q;
    
    vector<pair<int, pair<long long, long long>>> queries;

    Solution sol;
    while (q-- > 0) {
        int t;
        cin >> t;
        if (t == 1) {
            long long a, b;
            cin >> a >> b;
            queries.emplace_back(1, make_pair(a, b));
        } else {
            queries.emplace_back(2, make_pair(0, 0));
        }
    }
    auto res = sol.solve(queries);
    for (auto& r : res) {
        cout << r.first << ' ' << r.second << '\n';
    }
    return 0;
}